脚本宝典收集整理的这篇文章主要介绍了PHP之string之implode()函数使用,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
implode
Description
string implode ( string $glue , array $pieces )
string implode ( array $pieces )
//Join array elements with a glue string.
//用 glue 将一维数组的值连接为一个字符串。
Note:
Parameters
glue
- defaults to an empty string.
- 默认为空的字符串。
pieces
- The array of strings to implode.
- 你想要转换的数组。
Return Values
- Returns a string containing a string rePResentation of all the array elements in the same order, with the glue string between each element.
- 返回一个字符串,其内容为由 glue 分割开的数组的值。
Examples
<?php
/**
* Created by PhpStorm.
* User: zhangrongxiang
* Date: 2018/2/17
* Time: 下午10:52
*/
$array = array( '`lastname`', '`email`', '`phone`' );
$comma_separated = implode( ",", $array );
//`lastname`,`email`,`phone`
echo $comma_separated . PHP_EOL;
// Empty string when using an empty array:
echo '-------------------------' . PHP_EOL;
print_r( implode( 'hello', array() ) ); // string(0) ""
echo '-------------------------' . PHP_EOL;
$id_nums = array( 1, 6, 12, 18, 24 );
$id_nums = implode( ", ", $id_nums );
$SQLquery = "Select `name`,email,phone From `usertable` where user_id IN ($id_nums)";
//Select `name`,email,phone from `usertable` where user_id IN (1, 6, 12, 18, 24)
echo $sqlquery . PHP_EOL;
$sqlquery = "Select $comma_separated from `usertable` where user_id IN ($id_nums)";
//Select `lastname`,`email`,`phone` from `usertable` where user_id IN (1, 6, 12, 18, 24)
echo $sqlquery . PHP_EOL;
$ar = array( "hello", null, "world" );
echo implode( ',', $ar ) . PHP_EOL; // hello,,world
$picnames = array( "pic1.jpg", "pic2.jpg", "pic3.jpg", "pic4.jpg", "pic5.jpg", "pic6.jpg", "pic7.jpg" );
$allpics = implode( "|", array_slice( $picnames, 0, 5 ) );
//pic1.jpg|pic2.jpg|pic3.jpg|pic4.jpg|pic5.jpg
echo $allpics . PHP_EOL;
$test = implode( [ "one", 2, 3, "four", 5.67 ] );
echo $test . PHP_EOL;//one23four5.67
/////////////////////////////////////////////////////////////////////////////////////
class Foo {
protected $title;
public function __construct( $title ) {
$this->title = $title;
}
public function __toString() {
return $this->title;
}
}
$array = [
new Foo( 'foo' ),
new Foo( 'bar' ),
new Foo( 'qux' )
];
//foo; bar; qux
echo implode( '; ', $array );
See
All rights reserved
以上是脚本宝典为你收集整理的PHP之string之implode()函数使用全部内容,希望文章能够帮你解决PHP之string之implode()函数使用所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。