php 将数组项合并为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4626732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:44:52  来源:igfitidea点击:

Merge array items into string

phparraysstring

提问by James

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

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

回答by cristian

Use the implodefunction.

使用implode函数

For example:

例如:

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

回答by Diablo

Try this from the PHP manual (implode):

从 PHP 手册 ( 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) ""
?>

回答by hitmanDX

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

如果您只想连接数组中的所有字符串,那么您应该查看implode()

回答by bharath

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

will produce this one,two,three,four

将产生这个一、二、三、四

回答by Sr.PEDRO

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

您可以使用join。它是implode的别名,在我看来更具可读性:

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

回答by Buttle Butkus

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.

如果要将一组对象的字符串表示形式合并为一个字符串,可以对这些对象的数组使用内爆。对象的类只需要__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'

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 foreachand using echo $obj->get('name').

我发现这个技巧对于快速获取一组对象的字符串表示以在网页上显示很有用。无需使用foreach和 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.

编辑:这里有一个“集合”类的例子。最后我有 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.

回答by Mihai Toader

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

actually join is an alias for the implode function.

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

回答by cocacola09

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

$string .= $value .' ';

}

回答by GeekGirl

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

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

回答by MMOSolution.com

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

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

You can change pattern for get any result you want!

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