sadder
sadder

Reputation: 21

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

Upvotes: 2

Views: 183

Answers (4)

bcosca
bcosca

Reputation: 17555

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'
);

Upvotes: 1

codaddict
codaddict

Reputation: 455030

You can use the implode function.

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

Upvotes: 3

hacksteak25
hacksteak25

Reputation: 2039

just do a $myVar = implode('', $array);

Upvotes: 1

CrayonViolent
CrayonViolent

Reputation: 32532

can use implode

Upvotes: 3

Related Questions