我需要合并两个具有以下格式的数组:
array(9)
{
[0]=> array(1) { ["BLA"]=> string(7) "bis 050" }
[1]=> array(1) { ["BLA"]=> string(7) "bis 060" }
[2]=> array(1) { ["BLA"]=> string(7) "bis 070" }
[3]=> array(1) { ["BLA"]=> string(7) "bis 080" }
[4]=> array(1) { ["BLA"]=> string(7) "bis 090" }
[5]=> array(1) { ["BLA"]=> string(7) "bis 100" }
[6]=> array(1) { ["BLA"]=> string(7) "bis 110" }
[7]=> array(1) { ["BLA"]=> string(7) "bis 120" }
[8]=> array(1) { ["BLA"]=> string(6) "gr 120" }
}
array(5)
{
[0]=> array(2) {
["BLA"]=> string(7) "bis 050"
["Amount"]=> string(3) "832" }
[1]=> array(2) {
["BLA"]=> string(7) "bis 060"
["Amount"]=> string(3) "448" }
[2]=> array(2) {
["BLA"]=> string(7) "bis 090"
["Amount"]=> string(4) "1216" }
[3]=> array(2) {
["BLA"]=> string(7) "bis 100"
["Amount"]=> string(4) "1024" }
[4]=> array(2) {
["BLA"]=> string(7) "bis 110"
["Amount"]=> string(3) "896" }
}
我试过了array_merge()
,array_merge_recursive()
但是没有用。我的目标是将第二个键及其值从array2(Amount)写入第一个键(BLA)相同的数组1中。另外"Amount":"0"
,如果array2中没有相应的值,我想写。有什么办法用PHP做到这一点?
结果应如下所示:
Result:
{
[0]=> array(2) {
["BLA"]=> string(7) "bis 050"
["Amount"]=> string(3) "832" }
[1]=> array(2) {
["BLA"]=> string(7) "bis 060"
["Amount"]=> string(3) "448" }
[2]=> array(2) {
["BLA"]=> string(7) "bis 070"
["Amount"]=> string(1) "0" }
[3]=> array(2) {
["BLA"]=> string(7) "bis 080"
["Amount"]=> string(1) "0" }
[4]=> array(2) {
["BLA"]=> string(7) "bis 090"
["Amount"]=> string(4) "1216" }
[5]=> array(2) {
["BLA"]=> string(7) "bis 100"
["Amount"]=> string(4) "1024" }
[6]=> array(2) {
["BLA"]=> string(7) "bis 110"
["Amount"]=> string(3) "896" }
[7]=> array(2) {
["BLA"]=> string(7) "bis 120"
["Amount"]=> string(1) "0" }
[8]=> array(2) {
["BLA"]=> string(6) "gr 120"
["Amount"]=> string(1) "0" }
}
我整理了一个快速演示,似乎可以解决您的问题。让我知道您是否需要任何其他帮助。
<?php
$arrOne = array(
array("BLA" => "bis 050"),
array("BLA" => "bis 060"),
array("BLA" => "bis 070"),
array("BLA" => "bis 080"),
array("BLA" => "bis 090"),
array("BLA" => "bis 100"),
array("BLA" => "bis 110"),
array("BLA" => "bis 120"),
array("BLA" => "gr 120")
);
$arrTwo = array(
array("BLA" => "bis 050","Amount" => "832"),
array("BLA" => "bis 060","Amount" => "448"),
array("BLA" => "bis 090","Amount" => "1216"),
array("BLA" => "bis 100","Amount" => "1024"),
array("BLA" => "bis 110","Amount" => "896")
);
$arrOutput = array();
foreach($arrOne as $arrOneValue) {
$searchKey = $arrOneValue["BLA"];
foreach($arrTwo as $arrTwoValue) {
if($arrTwoValue["BLA"] == $searchKey) {
$arrOutput[] = array("BLA" => $searchKey, "Amount" => $arrTwoValue["Amount"]);
continue 2; // Continue the outer loop
}
}
// We didn't find the key
$arrOutput[] = array("BLA" => $searchKey, "Amount" => "0");
}
var_dump($arrOutput);
?>
产生如下内容:
array(9) {
[0]=> array(2) { ["BLA"]=> string(7) "bis 050" ["Amount"]=> string(3) "832" }
[1]=> array(2) { ["BLA"]=> string(7) "bis 060" ["Amount"]=> string(3) "448" }
[2]=> array(2) { ["BLA"]=> string(7) "bis 070" ["Amount"]=> string(1) "0" }
[3]=> array(2) { ["BLA"]=> string(7) "bis 080" ["Amount"]=> string(1) "0" }
[4]=> array(2) { ["BLA"]=> string(7) "bis 090" ["Amount"]=> string(4) "1216" }
[5]=> array(2) { ["BLA"]=> string(7) "bis 100" ["Amount"]=> string(4) "1024" }
[6]=> array(2) { ["BLA"]=> string(7) "bis 110" ["Amount"]=> string(3) "896" }
[7]=> array(2) { ["BLA"]=> string(7) "bis 120" ["Amount"]=> string(1) "0" }
[8]=> array(2) { ["BLA"]=> string(6) "gr 120" ["Amount"]=> string(1) "0" }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句