如何在 PHP 中合并 4 个不同的键和值数组


How to merge 4 different keys and values arrays in PHP?

这似乎是一个常见问题,但我找不到任何帮助...

我需要合并具有不同键和值的 4 个数组。所以有我的 4 个不同的数组:

array(7) {
  ["id"]=>
  string(2) "32"
  ["title"]=>
  string(7) "Blettes"
  ["product_type_id"]=>
  string(2) "43"
  ["quantity"]=>
  string(4) "1.00"
  ["price"]=>
  string(4) "2.80"
  ["created_at"]=>
  string(19) "2011-09-03 11:31:35"
  ["proposition_vente"]=>
  string(1) "4"
}
array(4) {
  ["id"]=>
  string(2) "32"
  ["achat"]=>
  string(2) "47"
  ["total_price"]=>
  string(18) "131.59999999999994"
  ["total_vat"]=>
  string(18) "6.8619999999999965"
}
array(2) {
  ["id"]=>
  string(2) "32"
  ["exposition"]=>
  string(2) "46"
}
array(3) {
  ["id"]=>
  string(2) "32"
  ["sale_queue_id"]=>
  string(3) "163"
  ["exposition"]=>
  string(2) "56"
}

进入这个:

array(7) {
  ["id"]=>
  string(2) "32"
  ["title"]=>
  string(7) "Blettes"
  ["product_type_id"]=>
  string(2) "43"
  ["quantity"]=>
  string(4) "1.00"
  ["price"]=>
  string(4) "2.80"
  ["created_at"]=>
  string(19) "2011-09-03 11:31:35"
  ["proposition_vente"]=>
  string(1) "4"
  ["achat"]=>
  string(2) "47"
  ["total_price"]=>
  string(18) "131.5"
  ["total_vat"]=>
  string(18) "6.86"
  ["exposition"]=>
  string(2) "46"
  ["sale_queue_id"]=>
  string(3) "163"
  ["exposition"]=>
  string(2) "56"
}

我知道我必须使用 id 才能正确合并它们。我尝试使用 foreach() 和 array_merge() 的组合,但没有成功。

欢迎任何帮助:)

$result = array_merge($arr1, $arr2, $arr3, $arr4) should work.

您也可以尝试以下操作:

$result = array();
$source = array($arr1, $arr2, $arr3, $arr4);
foreach ($source as $a) {
    $result = array_merge($result, $a);
}

我用这个弄清楚了:

foreach ($totalProposition as $key => $value) {
$test[$key]=array_merge($totalProposition[$key], $totalAchat[$key], $uniqueSale2[$key], $exposition2[$key]);    
}

感谢您的帮助:)