我需要合并两个具有以下格式的数组:
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)写入数组1,其中第一个键(BLA)的值相同。另外,如果array2中没有对应的值,我想写"Amount":"0"
。有没有办法用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" }
}
答案 0 :(得分: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" }
}
答案 1 :(得分:0)
您可以尝试这种方式:
$result = array_filter($array1, function (&$item) {
$item['Amount'] = '0';
return $item;
});
$index = array_column( $result,'BLA' );
foreach( $array2 as $row )
{
$found = array_search( $row['BLA'], $index );
if( $found === False )
{
$result[] = $row;
$index[] = $row['BLA'];
}
else
{
$result[$found]['Amount'] = $row['Amount'];
}
}
print_r( $result );
的 eval.in demo 强>
首先,我过滤数组1以向每个元素添加空Amount
;然后我创建一个BLA
值的索引,以避免嵌套foreach
循环。
通过第二个数组的foreach
循环,我发现密钥是否存在于修改过的数组中:如果它们存在,我会更改“数量”数字。键,否则我将一个新项添加到数组。
通过这种方法,您只执行两个foreach循环(将array_filter
视为foreach,三个也考虑array_column
); whit嵌套foreach
,相反,你必须执行可变数量的循环,如果你有大型数组,会影响性能。
array_column
在php&gt; = 5.5中可用。
如果您使用以前的版本,则必须将相应的行替换为:
$index = array();
foreach( $result as $row ) $index[] = $row['BLA'];
答案 2 :(得分:0)
试试这个:
$array1 = '[
{"BLA":"bis 050"},
{"BLA":"bis 060"},
{"BLA":"bis 070"},
{"BLA":"bis 080"},
{"BLA":"bis 090"},
{"BLA":"bis 100"},
{"BLA":"bis 110"},
{"BLA":"bis 120"},
{"BLA":"gr 120"}
]';
$array2 = '[
{"BLA":"bis 050","Amount":"832"},
{"BLA":"bis 060","Amount":"448"},
{"BLA":"bis 090","Amount":"1216"},
{"BLA":"bis 100","Amount":"1024"},
{"BLA":"bis 110","Amount":"896"}
]';
$a1 = json_decode($array1, TRUE);
$a2 = json_decode($array2, TRUE);
$result = [];
foreach ($a1 as $source) {
$found = FALSE;
foreach ($a2 as $key => $content) {
if ($content['BLA'] == $source['BLA']) {
$element = $content;
$a2[$key]['BLA'] = NULL; // ensure not to reuse furtherly
$found = TRUE;
break;
}
}
if (!$found) {
$element = ['BLA' => $source['BLA'], 'Amount' => 0];
}
$result[] = $element;
}
// add unused $a2 content, if any
if ($a2) {
$result += $a2;
}
echo '<pre>' . print_r(str_replace(",{", ",\n{", json_encode($result)), TRUE) . '</pre>';