我有这个数组
$zone = [{"Bertoua Zone 3":[{"code":"BFT50C","product_quantity":"4","zone_name":"Bertoua Zone 3"},{"code":"MNY65C","product_quantity":"10","zone_name":"Bertoua Zone 3"},{"code":"JAP65C","product_quantity":"7","zone_name":"Bertoua Zone 3"},{"code":"MNY65C","product_quantity":"4","zone_name":"Bertoua Zone 3"},{"code":"JAP65C","product_quantity":"3","zone_name":"Bertoua Zone 3"},{"code":"BFT50C","product_quantity":"3","zone_name":"Bertoua Zone 3"},{"code":"JAP65C","product_quantity":"2","zone_name":"Bertoua Zone 3"}],"Bertoua Zone 1":[{"code":"MNY65C","product_quantity":"5","zone_name":"Bertoua Zone 1"},{"code":"JAP65C","product_quantity":"3","zone_name":"Bertoua Zone 1"},{"code":"BFT50C","product_quantity":"7","zone_name":"Bertoua Zone 1"},{"code":"MNY65C","product_quantity":"15","zone_name":"Bertoua Zone 1"},{"code":"JAP65C","product_quantity":"5","zone_name":"Bertoua Zone 1"},{"code":"BFT50C","product_quantity":"4","zone_name":"Bertoua Zone 1"}]}]
我想要得到这个结果
$recapByZone = [{"Bertoua Zone 3":[{"code":"BFT50C","product_quantity":"7","zone_name":"Bertoua Zone 3"},{"code":"MNY65C","product_quantity":"14","zone_name":"Bertoua Zone 3"},{"code":"JAP65C","product_quantity":"12","zone_name":"Bertoua Zone 3"},],"Bertoua Zone 1":[{"code":"MNY65C","product_quantity":"20","zone_name":"Bertoua Zone 1"},{"code":"JAP65C","product_quantity":"8","zone_name":"Bertoua Zone 1"},{"code":"BFT50C","product_quantity":"11","zone_name":"Bertoua Zone 1"}]}]
我已尽力而为,它与您的示例代码略有不同。
参见https://onecompiler.com/php/3ybbmywrp
$zone = '[{"Bertoua Zone 3":[{"code":"BFT50C","product_quantity":"4","zone_name":"Bertoua Zone 3"},{"code":"MNY65C","product_quantity":"10","zone_name":"Bertoua Zone 3"},{"code":"JAP65C","product_quantity":"7","zone_name":"Bertoua Zone 3"},{"code":"MNY65C","product_quantity":"4","zone_name":"Bertoua Zone 3"},{"code":"JAP65C","product_quantity":"3","zone_name":"Bertoua Zone 3"},{"code":"BFT50C","product_quantity":"3","zone_name":"Bertoua Zone 3"},{"code":"JAP65C","product_quantity":"2","zone_name":"Bertoua Zone 3"}],"Bertoua Zone 1":[{"code":"MNY65C","product_quantity":"5","zone_name":"Bertoua Zone 1"},{"code":"JAP65C","product_quantity":"3","zone_name":"Bertoua Zone 1"},{"code":"BFT50C","product_quantity":"7","zone_name":"Bertoua Zone 1"},{"code":"MNY65C","product_quantity":"15","zone_name":"Bertoua Zone 1"},{"code":"JAP65C","product_quantity":"5","zone_name":"Bertoua Zone 1"},{"code":"BFT50C","product_quantity":"4","zone_name":"Bertoua Zone 1"}]}]';
$zone = json_decode($zone, true);
$zone2 = '[{"Bertoua Zone 3":[{"code":"BFT50C","product_quantity":"7","zone_name":"Bertoua Zone 3"},{"code":"MNY65C","product_quantity":"14","zone_name":"Bertoua Zone 3"},{"code":"JAP65C","product_quantity":"12","zone_name":"Bertoua Zone 3"}],"Bertoua Zone 1":[{"code":"MNY65C","product_quantity":"20","zone_name":"Bertoua Zone 1"},{"code":"JAP65C","product_quantity":"8","zone_name":"Bertoua Zone 1"},{"code":"BFT50C","product_quantity":"11","zone_name":"Bertoua Zone 1"}]}]';
$zone2 = json_decode($zone2, true);
$result = [];
foreach ($zone[0] as $key => $element) {
foreach ($element as $value) {
if (isset($result[$key][$value['code']])) {
$result[0][$key][$value['code']]['product_quantity'] += $value['product_quantity'];
} else {
$result[0][$key][$value['code']] = $value;
}
}
}
echo '<pre>';
echo print_r($result);
echo '</pre>';
echo '<hr>';
echo '<pre>';
echo print_r($zone2);
echo '</pre>';
遇到唯一的
code
值时,将新的组引用推入结果数组中。 否则,当之前遇到过 code
值时,请将 product_quantity
添加到组的 product_quantity
值中。
迭代数据子集后,用分组数据覆盖原始行数据。
我不知道你唯一的第一级是否有多个元素,但我会为此编写一个 foreach 循环,以防万一。
$payload = json_decode('[{"Bertoua Zone 3":[{"code":"BFT50C","product_quantity":"4","zone_name":"Bertoua Zone 3"},{"code":"MNY65C","product_quantity":"10","zone_name":"Bertoua Zone 3"},{"code":"JAP65C","product_quantity":"7","zone_name":"Bertoua Zone 3"},{"code":"MNY65C","product_quantity":"4","zone_name":"Bertoua Zone 3"},{"code":"JAP65C","product_quantity":"3","zone_name":"Bertoua Zone 3"},{"code":"BFT50C","product_quantity":"3","zone_name":"Bertoua Zone 3"},{"code":"JAP65C","product_quantity":"2","zone_name":"Bertoua Zone 3"}],"Bertoua Zone 1":[{"code":"MNY65C","product_quantity":"5","zone_name":"Bertoua Zone 1"},{"code":"JAP65C","product_quantity":"3","zone_name":"Bertoua Zone 1"},{"code":"BFT50C","product_quantity":"7","zone_name":"Bertoua Zone 1"},{"code":"MNY65C","product_quantity":"15","zone_name":"Bertoua Zone 1"},{"code":"JAP65C","product_quantity":"5","zone_name":"Bertoua Zone 1"},{"code":"BFT50C","product_quantity":"4","zone_name":"Bertoua Zone 1"}]}]', true);
$result = [];
foreach ($payload as $index => $zones) {
foreach ($zones as $zone => $rows) {
unset($ref);
$grouped = [];
foreach ($rows as $row) {
if (!isset($ref[$row['code']])) {
$ref[$row['code']] = $row;
$grouped[] =& $ref[$row['code']];
continue;
}
$ref[$row['code']]['product_quantity'] += $row['product_quantity'];
}
$result[$index][$zone] = $grouped;
}
}
var_export($result);