12

How do I merge all the array items into a single string?

3
  • 1
    Do you want you array items to be comma seperated?...if so you can $finalarr = array_merge($arr1, $arr2) and do an implode(",", $finalarr) hope this helps Commented Jan 7, 2011 at 14:56
  • have a look at implode Commented Jan 7, 2011 at 14:57
  • i did array_merge because i misunderstood you question....when you said merge all array. but implode is certainly your answer.. Commented Jan 7, 2011 at 15:01

10 Answers 10

44

Use the implode function.

For example:

$fruits = array('apples', 'pears', 'bananas'); echo implode(',', $fruits); 
Sign up to request clarification or add additional context in comments.

Comments

7

Try this from the PHP manual (implode):

<?php $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); echo $comma_separated; // lastname, email, and phone // Empty string when using an empty array: var_dump(implode('hello', array())); // string(0) "" ?> 

Comments

5

If you are trying to just concatenate all of strings in the array, then you should look at implode().

Comments

4
$array1 = array( "one", "two", ) $array2 = array( "three", "four", ) $finalarr = array_merge($array1, $array2); $finalarr = implode(",", $finalarr); 

will produce this one,two,three,four

Comments

3

You can use join. It's an alias for implode, and in my opinion more readable:

$fruits = array('apples', 'pears', 'bananas'); echo join(',', $fruits); 

1 Comment

join might be more readable, but implode is certainly more exciting.
1
join(" -- ", Array("a", "b")) == "a -- b" 

actually join is an alias for the implode function.

Comments

1

If you want to merge the string representation of a group of objects into a single string, you can use implode on an array of those objects. The objects' classes just have to have the __toString() magic method defined.

class myClass { protected $name; protected $value; public function __construct($name,$value) { $this->name = $name; $this->value = $value; } public function __toString() { return $this->name . '/' . $this->value; } } $obj1 = new myClass('one',1); $obj2 = new myClass('two',2); $obj_array = array($obj1, $obj2); $string_of_all_objects = implode('|',$obj_array); echo $string_of_all_objects; // 'one/1|two/2' 

I found that trick useful to quickly get a string representation of a group of objects for display on a webpage. No need to loop through the object array with foreach and using echo $obj->get('name').

EDIT: And here's and example with a "collection" class. I have 2 outputs (echos) at the end. The 2nd one should work, but I'm not sure about the 1st.

class myCollectionClass implements IteratorAggregate { protected $objects = array(); public function __construct() {}; public function add(myClass $object) { $this->objects[] = $object; return $this; // fluid } public function getIterator() { // for the interface return new ArrayIterator($this->objects); } public function __toString() { return implode($this->objects); } } $my_collection = new myCollectionClass(); $my_collection->add($obj1)->add($obj2); // add both myClass objects to the collection. can do in one line because fluid //echo implode('|',$my_collection); // Comment out myCollectionClass's __toString method to test this. does it work? I'm not sure. But the next line will work thanks to myCollectionClass' __toString, which itself uses myClass's __toString echo $my_collection; // same output as previous block before EDIT. 

Comments

0
$array= array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" ); $string=""; foreach ( $tempas $array) { $string=$string.",".$temp; } 

1 Comment

+1 This does everything the question asks for in a good way. The coincidence that there is a more optimal way does not make this answer unhelpful.
0
foreach($array as $key => $value) { $string .= $value .' '; } 

7 Comments

slow ... and what does "string" mean? and why extra space at the end of resulting string?
error, changed - fixed. how does implode work, and i dont mean the arguments. I'm refering to the function itself. please tell me.
implode will still iterate through the array.
I fully admit implode is better solution but his response was that its slow. which is completely wrong. so I was correcting him. I fully admit implode is better for organisation of code.
+1 This does everything the question asks for in a good way. The coincidence that there is a more optimal way does not make this answer unhelpful.
|
0

For Multi Array such as:

 $multi_arrays = array( 0 => array('model' => 'Product 1'), 1 => array('model' => 'Product 2'), 2 => array('model' => 'Product 3'), 3 => array('model' => 'Product 4')); $pattern = array('/\[/', '/\]/', '/{"model":/', '/}/', '/\"/'); $str_models = preg_replace($pattern, '', json_encode( $multi_arrays)); 

The result will be:

Product 1, Product 2, Product 3, Product 4

You can change pattern for get any result you want!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.