开发者

Merge array items into string

How do I merge all the arra开发者_运维知识库y items into a single string?


Use the implode function.

For example:

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


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) ""
?>


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


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

will produce this one,two,three,four


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

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


join(" -- ", Array("a", "b")) == "a -- b"

actually join is an alias for the implode function.


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.


$array= array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );
$string="";

foreach ( $tempas $array) {
  $string=$string.",".$temp;
}


foreach($array as $key => $value) {

$string .= $value .' ';

}


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜