如何递归地连接数组元素

时间:2016-01-20 08:43:59

标签: php

我正在处理一个项目并且我已经堆积了2天,这是我的问题:我有两个arrays并想要检索 Array_2 和concatenate它与PHP中第一个 Array_1 中每个对象的内容。

Array_1

 [[1453274700000,24011],[1453275000000,24222],[1453275300000,24284],[1453275600000,24331],...]

Array_2

[[1453274700000,51951],[1453275000000,52093],[1453275300000,52251],[1453275600000,52288],...] 

Wanted_array

[[1453274700000,24011,51951],[1453275000000,24222,52093],[1453275300000,24284,52251],[1453275600000,24331,52288]...]

3 个答案:

答案 0 :(得分:1)

功能性解决方案:

$result = array_map(function (array $a1, array $a2) {
    return array_merge($a1, [$a2[1]]);
}, $array_1, $array_2);

这假设所有项目都是有序的,只需按其订单合并,而不是按其第一个值合并。

答案 1 :(得分:1)

使用array_walk并添加$array2中的第二项(如果存在)。

$array1 = array(
    array(1453274700000,24011),
    array(1453275000000,24222),
    array(1453275300000,24284),
    array(1453275600000,24331)
    );

$array2 = array(
    array(1453274700000,51951),
    array(1453275000000,52093),
    array(1453275300000,52251),
    array(1453275600000,52288),
    );


array_walk($array1, function(&$item, $key) use ($array2){
    if(isset($array2[$key][1])){
        $item[] = $array2[$key][1];
    }
});

print_r($array1);

<强>输出

Array
(
    [0] => Array
        (
            [0] => 1453274700000
            [1] => 24011
            [2] => 51951
        )

    [1] => Array
        (
            [0] => 1453275000000
            [1] => 24222
            [2] => 52093
        )

    [2] => Array
        (
            [0] => 1453275300000
            [1] => 24284
            [2] => 52251
        )

    [3] => Array
        (
            [0] => 1453275600000
            [1] => 24331
            [2] => 52288
        )

)

修改

正如@ h2ooooooo所指出的那样,数组项可能是随机顺序的。如果数组项可以是随机顺序并且它们与第一个索引值匹配,请使用此项(使用PHP&gt; = 5.5.0):

$array1 = array(
    array(1453274700000,24011),
    array(1453275000000,24222),
    array(1453275300000,24284),
    array(1453275600000,24331),
    array(1453276000000,24222) // no match in $array2
    );

$array2 = array(
    array(1453275000000,52093),
    array(1453274700000,51951),
    array(1453275300000,52251),
    array(1453275600000,52288),
    );


array_walk($array1, function(&$item, $key) use ($array2){
    // Find match in $array2
    $array2_key = array_search($item[0], array_column($array2, 0));
    // If match found
    if($array2_key !== false && isset($array2[$array2_key][1])){
        $item[] = $array2[$array2_key][1];
    }
    // No match
    else{
        $item[] = null;
    }
});

print_r($array1);

<强>输出

Array
(
    [0] => Array
        (
            [0] => 1453274700000
            [1] => 24011
            [2] => 51951
        )

    [1] => Array
        (
            [0] => 1453275000000
            [1] => 24222
            [2] => 52093
        )

    [2] => Array
        (
            [0] => 1453275300000
            [1] => 24284
            [2] => 52251
        )

    [3] => Array
        (
            [0] => 1453275600000
            [1] => 24331
            [2] => 52288
        )

    [4] => Array
        (
            [0] => 1453276000000
            [1] => 24222
            [2] => 
        )

)

答案 2 :(得分:1)

如果您希望$item[0]定义&#34; group&#34;每个值都属于,您可以遍历第一个数组并保存$item[0]作为键,$item[1]作为值。对第二个数组执行相同的操作。现在遍历已保存的array1数组,并检查array2的已保存数组是否包含相同的键。对array2执行相同的操作(如果它具有array1没有的键),并将其全部保存到新数组中:

<?php

$arr1 = array(
        array('1453274700000',24011),
        array('1453275000000',24222),
        array('1453276000000',24222), // inexistent in $arr2
    );

$arr2 = array(
        array('1453275000000',52093),
        array('1453274700000',51951),
        array('1453273000000',24222), // inexistent in $arr1
    );

$arr1dictionary = [];
$arr2dictionary = [];
$result = [];

foreach ($arr1 as $collection) {
  $arr1dictionary[$collection[0]] = $collection[1];
}

foreach ($arr2 as $collection) {
  $arr2dictionary[$collection[0]] = $collection[1];
}

foreach ($arr1dictionary as $key => $value) {
  if (isset($arr2dictionary[$key])) {
    $result[$key] = [$key, $value, $arr2dictionary[$key]];
  } else {
    $result[$key] = [$key, $value, null];
  }
}

foreach ($arr2dictionary as $key => $value) {
  if (isset($result[$key])) {
    continue;
  }

  $result[$key] = [$key, null, $value];
}

$result = array_values($result);

print_r($result); 

<强>输出

Array
(
    [0] => Array
        (
            [0] => 1453274700000
            [1] => 24011
            [2] => 51951
        )

    [1] => Array
        (
            [0] => 1453275000000
            [1] => 24222
            [2] => 52093
        )

    [2] => Array
        (
            [0] => 1453276000000
            [1] => 24222
            [2] => (null, the value only exists in $arr1)
        )

    [3] => Array
        (
            [0] => 1453273000000
            [1] => (null, the value only exists in $arr2)
            [2] => 24222
        )

)

DEMO