通过子数组中的键连接2个多维数组

时间:2017-02-21 08:59:54

标签: php arrays multidimensional-array

我有2个多维数组:

数组1:

Array
(
    [0] => Array
    ( 
      [symbol] => ABN
      [chf] => 11.20
      [percent] => 0.16
      [change] => 0.798168794168579
    )
    ...
    [1295] => Array
    ( 
      [symbol] => ZURN
      [chf] => 19.81
      [percent] => -0.35
      [change] => 0.06909317389138
    )
    [1296] => Array
    ( 
      [symbol] => 
      [chf] => 3.11
      [percent] => 0.11
      [change] => 0.798324232168579
    )
)

数组2:

Array
(
    [0] => stdClass Object
    (  
      [symbol] => UBSG
      [smi] => 1
      [dax] => 0
      [dow] => 0
    )
    ...
    [21] => stdClass Object
    (  
      [symbol] => XYZN
      [smi] => 0
      [dax] => 1
      [dow] => 0
    )
    [22] => stdClass Object
    (  
      [symbol] => ZURN
      [smi] => 0
      [dax] => 0
      [dow] => 1
    )
)

我需要什么:

具有以下键/值对的新阵列3:

  • Array1.symbol
  • Array1.chf
  • Array1.percent
  • Array1.change
  • Array2.smi
  • Array2.dax
  • Array2.dow

WHERE Array1.symbol == Array2.symbol

结果将是:

Array
(
    [0] => Array
    (
      [symbol] => ZURN
      [chf] => 19.81
      [percent] => -0.35
      [change] => 0.06909317389138
      [smi] => 0
      [dax] => 0
      [dow] => 1
    )
)

我想可以用array_intersect之类的东西来完成,但我真的不知道。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

使用array_uintersectjson_encodejson_decodearray_merge函数的解决方案:

// assuming $arr1 and $arr2 are your first and second array respectively

// transforming second array into array of arrays for a proper comparison
$arr2_transformed = json_decode(json_encode($arr2), true);
$result = [];

array_uintersect($arr1, $arr2_transformed, function ($a, $b) use(&$result){
    $comparison = strcmp($a['symbol'], $b['symbol']);
    if ($comparison === 0) {    // if `symbols` are equal
        $result[] = array_merge($a, json_decode(json_encode($b), true));
    }
    return $comparison;    
});

print_r($result);

输出如下:

Array
(
    [0] => Array
        (
            [symbol] => ZURN
            [chf] => 19.81
            [percent] => -0.35
            [change] => 0.06909317389138
            [smi] => 0
            [dax] => 0
            [dow] => 1
        )
    ....

)

DEMO link

答案 1 :(得分:1)

这是一个首先为Array2构建查找表,然后将Array2中的字段添加到Array1的解决方案。

// build a lookup table of Array2 keyed by symbol
$lookup = array_combine(
    array_map(function($item) {
        return $item->symbol;
    }, $arr2), $arr2);
// add matching symbol fields from Array2 to Array1
$result = array_map(function($item) use ($lookup) {
    $symbol = $item["symbol"];
    return isset($lookup[$symbol]) ?
        array_merge($item, (array)$lookup[$symbol]) : $item;
}, $arr1);

试试online

要省略没有匹配符号的值,请按以下步骤修改:

$result = array_filter(
    array_map(function($item) use ($lookup) {
        $symbol = $item["symbol"];
        return isset($lookup[$symbol]) ?
            array_merge($item, (array)$lookup[$symbol]) : null;
    }, $arr1));

试试online

答案 2 :(得分:0)

希望这个帮助:使用array_merge组合数组。

这里你的第二个包含对象的数组首先将其转换为数组

试试这个:

$ arr1< ------- 1st array

$ arr2< -------第二阵列

    foreach ($arr1 as $key=>$value) {
        $result[$key]=array_merge((array)$arr2[$key],$arr1[$key]);    
    }

var_dump($result);