简体   繁体   中英

Concatenate the elements in a PHP array

How can I turn the following array below so it looks like example 2 using PHP.

Example 1 array.

Array ( [0] => &sub1=a1 [1] => &sub2=aa [2] => &sub3=glass-and-mosaics [3] => &sub4=guides-and-reviews [4] => &sub5=silent-movies [5] => &sub6=even-more-eastern-religions-and-philosophies ) 

Example 2.

&sub1=a1&sub2=aa&sub3=glass-and-mosaics&sub4=guides-and-reviews&sub5=silent-movies&sub6=even-more-eastern-religions-and-philosophies

可以使用内

You can use the implode function.

$arr = array(0 => '&sub1=a1',1 => '&sub2=aa');
$str = implode('',$arr);

只做一个$myVar = implode('', $array);

If this is a query fragment of a URL, use http_build_query instead:

echo http_build_query(
   'sub1'=>'a1',
   'sub2'=>'aa',
   'sub3'=>'glass-and-mosaics',
   'sub4'=>'guides-and-reviews',
   'sub5'=>'silent-movies',
   'sub6'=>'even-more-eastern-religions-and-philosophies'
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM