PHP之string之implode()函数使用
implode
- (PHP 4, PHP 5, PHP 7)
- implode — Join array elements with a string
- implode — 将一个一维数组的值转化为字符串
Description
string implode ( string $glue , array $pieces )
string implode ( array $pieces )
//Join array elements with a glue string.
//用 glue 将一维数组的值连接为一个字符串。
Note:
- implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments.
- 因为历史原因,implode() 可以接收两种参数顺序,但是 explode() 不行。不过按文档中的顺序可以避免混淆。
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源码阅读]explode和implode函数
explode和implode函数主要用作字符串和数组间转换的操作,比如获取一段参数后根据某个字符分割字符串,或者将一个数组的结果使用一个字符合并成一个字符串输出.在PHP中经常会用到这两个函数,因此 ...
- PHP implode() 函数 把数组元素组合为字符串
http://www.w3school.com.cn/php/func_string_implode.asp PHP implode() 函数 PHP String 函数 实例 把数组元素组合为字符串 ...
- join()、implode()函数
join() 函数 join() 函数把数组元素组合为一个字符串. join() 函数是 implode() 函数的别名. 语法 join(separator,array) 参数 描述 separat ...
- ***PHP implode() 函数,将数组合并为字符串;explode() 函数,把字符串打散为数组
实例 把数组元素组合为字符串: <?php $arr = array('Hello','World!','I','love','Shanghai!'); echo implode(" ...
- PHP源码阅读笔记一(explode和implode函数分析)
PHP源码阅读笔记一一.explode和implode函数array explode ( string separator, string string [, int limit] )此函数返回由字符 ...
- php implode()函数详解
php implode()函数的作用? php 中implode() 函数是返回一个由数组元素组合成的字符串,它与php explode()函数的作用是相反的,php explode() 函数是:使用 ...
- implode函数的升级版,将一个多维数组的值转化为字符串
/** * implode函数的升级版 * 将一个多维数组的值转化为字符串 * @param $glue * @param $data * @return string */function mult ...
- php中explode和implode函数
explode array explode ( string $delimiter, string $string, [ , $limit ] ) 函数返回由字符串组成的数组,每个元素都是string ...
- ZH奶酪:PHP解析URL及parse_url、parse_str、explode、implode函数说明
首先看一下解析任意URL的代码: (1)获取协议类型:例如参考链接中的:http (2)获取主机地址:例如参考链接中的:my.oschina.net (3)获取当前页面在服务器的路径:例如参考链接中的 ...
随机推荐
- 查看JVM内存使用情况
Runtime run = Runtime.getRuntime(); long max = run.maxMemory()/(1024*1024); long total = run.totalMe ...
- How to extract msu/msp/msi/exe files from the command line
http://www.windowswiki.info/2009/02/19/how-to-extract-msumspmsiexe-files-from-the-command-line/ Micr ...
- Mustache 使用说明
Mustache 使用说明 最近在升级SinGooCMS到MVC架构.管理前端使用了Mustache模板,把使用心得记录一下! 一.官网http://mustache.github.io/https: ...
- 解决vs2017调试出现脚本错误(/Community/Common7/IDE/PrivateAssemblies/plugin.vs.js) 方法
原文地址:http://bkcoding.cn/post_1204.html 新装的vs2017编译时出现当前页面脚本错误 url:/Community/Common7/IDE/PrivateAsse ...
- NOI2009 区间
题目链接:戳我 60分部分分还是很好拿的,排序(按照左端点为第一关键字,右端点为第二关键字)之后一个\(O(n^2)\),暴力判交,更新最小值,就可以水过前12个测试点. #include<io ...
- Kubernetes 集群安装部署
etcd集群配置 master节点配置 1.安装kubernetes etcd [root@k8s ~]# yum -y install kubernetes-master etcd 2.配置 etc ...
- NOIP2013PUZZLE
#include<cstdio> #include<cstring> #define MIN(A,B) (A)<(B)?(A):(B) using namespace s ...
- J - Judge(快速幂)(同余定理)
J - Judge Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit S ...
- 【文文殿下】CF1029F Multicolored Markers
这道题考场上卡了文文相当长的时间,所以写个题解泄泄愤QAQ 题意:给你$a$块红瓷砖,$b$块白瓷砖,在一个无限大的地上拼装,要求整体是一个矩形,并且至少有一种颜色是一个矩形,求最小周长. 题解: 首 ...
- [ActionScript3.0] AS3利用ExternalInterface与js通信
AS3代码,可做文档类; package { import flash.display.Sprite; import flash.events.*; import flash.external.Ext ...