如何通过特殊字符连接php中的两个数组元素值?

时间:2013-08-05 08:46:38

标签: php arrays concatenation

我有以下两个阵列

Array
(
    [0] => 2013-07-09
    [1] => 2013-07-16
    [2] => 2013-07-23
    [3] => 2013-07-30
)

Array
(
    [0] => 2013-07-16
    [1] => 2013-07-23
    [2] => 2013-07-30
    [3] => 2013-08-06

)

我想通过下面的特殊character.given输出连接两个数组元素值:

Array
(
    [0] => 2013-07-09 : 2013-07-16
    [1] => 2013-07-16 : 2013-07-23
    [2] => 2013-07-23 : 2013-08-30
    [3] => 2013-08-30 : 2013-08-06

)

3 个答案:

答案 0 :(得分:10)

尝试array_map这样的

$combined = array_map(function($a, $b) { return $a . ' : ' . $b; }, $array1, $array2));

答案 1 :(得分:1)

$a1 = new ArrayIterator($array1);
$a2 = new ArrayIterator($array2);

$it = new MultipleIterator;

$it->attachIterator($a1);

$it->attachIterator($a2);

foreach($it as $e) {

        $array3[] = $e[0]." : ".$e[1]);
}

答案 2 :(得分:1)

执行此操作,$combined_array是您的答案

$array1 = Array
(
    [0] => 2013-07-09
    [1] => 2013-07-16
    [2] => 2013-07-23
    [3] => 2013-07-30
);
$array2 = Array
(
    [0] => 2013-07-16
    [1] => 2013-07-23
    [2] => 2013-07-30
    [3] => 2013-08-06

);
$combined_array = array();
foreach($array1 as $key=>$value)
{
    $combined_array[$key]=$value." : ".$array2[$key];
}