Php 将数组项合并到字符串中

Php 将数组项合并到字符串中,php,arrays,string,Php,Arrays,String,如何将所有数组项合并为一个字符串?使用 例如: $fruits = array('apples', 'pears', 'bananas'); echo implode(',', $fruits); 实际上,join是内爆函数的别名 从PHP手册()中尝试以下内容: foreach($array as $key => $value) { $string .= $value .' '; } 将产生一、二、三、四个如果您试图连接数组中的所有字符串,则应查看。如果要将一组对象的字符串表

如何将所有数组项合并为一个字符串?

使用

例如:

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

实际上,join是内爆函数的别名

从PHP手册()中尝试以下内容:

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

$string .= $value .' ';

}


将产生一、二、三、四个

如果您试图连接数组中的所有字符串,则应查看。

如果要将一组对象的字符串表示形式合并为单个字符串,可以对这些对象的数组使用内爆。对象的类只需定义
\uuu toString()
魔术方法即可

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'
我发现这个技巧对于快速获取一组对象的字符串表示以显示在网页上非常有用。无需使用
foreach
并使用
echo$obj->get('name')
遍历对象数组

编辑:下面是一个“collection”类的示例。我有2个输出(回声)在最后。第二个应该行,但我不确定第一个是否行

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.
你可以用。这是一个别名,在我看来更具可读性:

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

对于多阵列,例如:

 $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));
结果将是:

产品1、产品2、产品3、产品4


您可以更改模式以获得您想要的任何结果

您希望数组项以逗号分隔吗?…如果是这样,您可以$finalarr=array\u merge($arr1,$arr2)并执行内爆(“,”,$finalarr)希望这有助于查看我执行了array\u merge,因为我误解了您的问题…当您说合并所有数组时。但内爆肯定是你的答案…慢。。。“字符串”是什么意思?为什么在结果字符串的末尾有额外的空间?错误,已更改-已修复。内爆是如何工作的,我不是指争论。我指的是函数本身。请告诉我。内爆仍然会在数组中迭代。我完全承认内爆是更好的解决方案,但他的反应是它很慢。这是完全错误的。所以我在纠正他。我完全承认内爆对代码的组织更好。+1这以一种很好的方式满足了问题的所有要求。巧合的是,有一种更为理想的方式,这并不会使这个答案毫无用处。巧合的是,有一种更为优化的方法并没有使这个答案毫无用处。
join
可能更具可读性,但
内爆
肯定更令人兴奋。
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'
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.
$fruits = array('apples', 'pears', 'bananas');
echo join(',', $fruits);
 $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));