我有一个多维数组来存储特定产品的数据,但是当程序删除它时,我收到了数量为(例如1)的产品和数量为(-1)的相同产品的新数据。从他们两个的数组。
array(3) {
[0]=>
array(6) {
["I_ART"]=>
string(14) "Еспресо"
["N_ART"]=>
int(405)
["C_ART"]=>
string(5) "1.900"
["Q_ART"]=>
string(7) "-1.0000"
["MASA"]=>
int(5)
["POR"]=>
int(2)
}
[1]=>
array(6) {
["I_ART"]=>
string(8) "Фреш"
["N_ART"]=>
int(363)
["C_ART"]=>
string(5) "4.100"
["Q_ART"]=>
string(6) "2.0000"
["MASA"]=>
int(5)
["POR"]=>
int(1)
}
[2]=>
array(6) {
["I_ART"]=>
string(14) "Еспресо"
["N_ART"]=>
int(405)
["C_ART"]=>
string(5) "1.900"
["Q_ART"]=>
string(6) "1.0000"
["MASA"]=>
int(5)
["POR"]=>
int(1)
}
}
例如,在此数组中,我要删除数组键0和2。此外,比较必须通过N_ART数字和数量差1负和1正来进行。 预期的输出是
array(1) {
[1]=>
array(6) {
["I_ART"]=>
string(8) "Фреш"
["N_ART"]=>
int(363)
["C_ART"]=>
string(5) "4.100"
["Q_ART"]=>
string(6) "2.0000"
["MASA"]=>
int(5)
["POR"]=>
int(1)
}
}
答案 0 :(得分:0)
这是我的建议:
$newItems = [];
// Here you iterate over your source array and sum quantities under same `N_ART`
foreach ($sourceArray as $item) {
$id = $item['N_ART'];
if (!isset($newItems[$id])) {
$newItems[$id] = $item;
} else {
$newItems[$id]['Q_ART'] += $item['Q_ART'];
}
}
// Here you filter array so as to remove items where `Q_ART` is 0 or negative
$newItems = array_filter($newItems, function ($item) { return 0 < $item['Q_ART']; });