【问题标题】:How can I merge two arrays (of array values) based on key value?如何根据键值合并两个数组(数组值)?
【发布时间】:2020-12-01 16:03:46
【问题描述】:

我正在尝试合并这两个数组:

$array1 = array (
  123 => array (
    'minutes_watched' => 192.0,
    'impressions_count' => 18
  ),
  456 => array (
    'minutes_watched' => 200.0,
    'impressions_count' => 20
  )
);

$array2 = array (
  123 => array (
    'ingested_trailers_count' => 3,
    'ingested_shorts_count' => 2,
    'ingested_features_count' => 1
  ),
  456 => array (
    'ingested_trailers_count' => 10,
    'ingested_shorts_count' => 10,
    'ingested_features_count' => 10
  )
);

我想这样结束:

$merged = array (
  123 => array (
    'minutes_watched' => 192.0,
    'impressions_count' => 18,
    'ingested_trailers_count' => 3,
    'ingested_shorts_count' => 2,
    'ingested_features_count' => 1
  ),
  456 => array (
    'minutes_watched' => 200.0,
    'impressions_count' => 20,
    'ingested_trailers_count' => 10,
    'ingested_shorts_count' => 10,
    'ingested_features_count' => 10
  )
);

最终有了这个:

$merged = array (
  array(
    'id' => 123 
    'minutes_watched' => 192.0,
    'impressions_count' => 18,
    'ingested_trailers_count' => 3,
    'ingested_shorts_count' => 2,
    'ingested_features_count' => 1
  ),
  array(
    'id' => 456 
    'minutes_watched' => 200.0,
    'impressions_count' => 20,
    'ingested_trailers_count' => 10,
    'ingested_shorts_count' => 10,
    'ingested_features_count' => 10
  )
);

如果没有一些笨拙的 for 循环,我似乎找不到一种简洁的方法。肯定有一些 PHP 数组函数可以处理这个问题吗?

【问题讨论】:

标签: php arrays


【解决方案1】:

你可以在一个循环中尝试这样的事情。参见 cmets 的解释。输出:

array(2) {
  [0]=>
  array(6) {
    ["minutes_watched"]=>
    float(192)
    ["impressions_count"]=>
    int(18)
    ["ingested_trailers_count"]=>
    int(3)
    ["ingested_shorts_count"]=>
    int(2)
    ["ingested_features_count"]=>
    int(1)
    ["id"]=>
    int(123)
  }
  [1]=>
  array(6) {
    ["minutes_watched"]=>
    float(200)
    ["impressions_count"]=>
    int(20)
    ["ingested_trailers_count"]=>
    int(10)
    ["ingested_shorts_count"]=>
    int(10)
    ["ingested_features_count"]=>
    int(10)
    ["id"]=>
    int(456)
  }
}

代码:

<?php

// Your input array.
$array1 = array (
  123 => array (
    'minutes_watched' => 192.0,
    'impressions_count' => 18
  ),
  456 => array (
    'minutes_watched' => 200.0,
    'impressions_count' => 20
  )
);

$array2 = array (
  123 => array (
    'ingested_trailers_count' => 3,
    'ingested_shorts_count' => 2,
    'ingested_features_count' => 1
  ),
  456 => array (
    'ingested_trailers_count' => 10,
    'ingested_shorts_count' => 10,
    'ingested_features_count' => 10
  )
);

// Output array
$output = [];

// Loop over first array, assuming all keys from $array1
// exist in $array2.
foreach ($array1 as $key => $child)
{
    // Combine the two arrays, preserving keys.
    $merged = $array1[$key] + $array2[$key];
    // Copy key to 'id' element.
    $merged['id'] = $key;
    // Add to output.
    $output[] = $merged;
}

var_dump($output);

/*
array(2) {
  [0]=>
  array(6) {
    ["minutes_watched"]=>
    float(192)
    ["impressions_count"]=>
    int(18)
    ["ingested_trailers_count"]=>
    int(3)
    ["ingested_shorts_count"]=>
    int(2)
    ["ingested_features_count"]=>
    int(1)
    ["id"]=>
    int(123)
  }
  [1]=>
  array(6) {
    ["minutes_watched"]=>
    float(200)
    ["impressions_count"]=>
    int(20)
    ["ingested_trailers_count"]=>
    int(10)
    ["ingested_shorts_count"]=>
    int(10)
    ["ingested_features_count"]=>
    int(10)
    ["id"]=>
    int(456)
  }
}
*/

【讨论】:

  • 这很接近,但我不能假设每个键都存在于任一数组中。
【解决方案2】:

我最终得到了这个解决方案:

  $merged = array_replace_recursive($arrayOne, $arrayTwo);

  $flattened = array_map(function($key, $value) {
      return array_merge($value, [
          'id' => $key
      ]);
  }, array_keys($merged), $merged);

我对@9​​87654322@ 部分感觉不太好,但是...

【讨论】:

  • 这是很多不必要的嵌套循环。
猜你喜欢
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 2016-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多