how to concatenate two array element values in php by special charcter?

我只是一个虾纸丫 提交于 2020-01-03 03:10:43

问题


i have two arrays given below

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

)

i want to concatenate two array element values by special character.given output below:

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

)

回答1:


Try with array_map like this

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



回答2:


$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]);
}



回答3:


do this , $combined_array is your answer

$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];
}


来源:https://stackoverflow.com/questions/18053956/how-to-concatenate-two-array-element-values-in-php-by-special-charcter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!