PHP之string之implode()函數使用

來源:互聯網
上載者:User

標籤:des   ever   foo   sel   prot   protect   order   his   hone   

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() 不行。不過按文檔中的順序可以避免混淆。
Parametersglue
  • 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.jpgecho $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; quxecho implode( '; ', $array );
See
  • http://php.net/manual/en/function.implode.php
All rights reserved

PHP之string之implode()函數使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.