Array
(
[INV828] => Array
(
[0] => shams interior
[org_name] => shams interior
[1] => INV828
[inv_no] => INV828
[2] => 1556
[productid] => 1556
[3] => E001-4ft
[productname] => E001-4ft
[4] => Paharganj
[warehousename] => Paharganj
[5] => 2018-04-19 16:55:05
[date] => 2018-04-19 16:55:05
[6] => 56
[quantity_consumed] => 256
[7] => 80364
[invoiceid] => 80364
)
[INV828] => Array
(
[0] => shams interior
[org_name] => shams interior
[1] => INV828
[inv_no] => INV828
[2] => 1556
[productid] => 1556
[3] => E001-4ft
[productname] => E001-4ft
[4] => Paharganj
[warehousename] => Paharganj
[5] => 2018-04-19 16:55:05
[date] => 2018-04-19 16:55:05
[6] => 56
[quantity_consumed] => 56
[7] => 80364
[invoiceid] => 80364
)
)
我需要在数组[quantity_consumed]
的键相同的地方添加[INV828]
。我想添加256 + 56的[quantity_consumed]
值并将其设为1。< / p>
答案 0 :(得分:0)
如果您有一个键值相同的关联数组(例如[INV828]),则最新条目将覆盖现有条目。
有关更多信息,请参见:PHP Manual Arrays
因此您的阵列将只有以下条目:
[INV828] => Array
(
[0] => shams interior
[org_name] => shams interior
[1] => INV828
[inv_no] => INV828
[2] => 1556
[productid] => 1556
[3] => E001-4ft
[productname] => E001-4ft
[4] => Paharganj
[warehousename] => Paharganj
[5] => 2018-04-19 16:55:05
[date] => 2018-04-19 16:55:05
[6] => 56
[quantity_consumed] => 56
[7] => 80364
[invoiceid] => 80364
)
答案 1 :(得分:0)
尝试一下:-
$arr = array(
array('org_name' => 'shams interior','inv_no' => 'INV828','productid' => 1556,'productname' => 'E001-4ft','warehousename' => 'Paharganj','date' => '2018-04-19 16:55:05','quantity_consumed' => 256,'invoiceid' => 80364),
array('org_name' => 'shams interior','inv_no' => 'INV829','productid' => 1556,'productname' => 'E001-4ft','warehousename' => 'Paharganj','date' => '2018-04-19 16:55:05','quantity_consumed' => 256,'invoiceid' => 80364),
array('org_name' => 'shams interior','inv_no' => 'INV828','productid' => 1556,'productname' => 'E001-4ft','warehousename' => 'Paharganj','date' => '2018-04-19 16:55:05','quantity_consumed' => 56,'invoiceid' => 80364),
array('org_name' => 'shams interior','inv_no' => 'INV829','productid' => 1556,'productname' => 'E001-4ft','warehousename' => 'Paharganj','date' => '2018-04-19 16:55:05','quantity_consumed' => 256,'invoiceid' => 80364)
);
$newArray = array();
foreach($arr as $k => $data) {
if (array_key_exists($data['inv_no'], $newArray)) { // If inv_no key is already exists, then just add the consumed quantity
$newArray[$data['inv_no']]['quantity_consumed'] = $newArray[$data['inv_no']]['quantity_consumed'] + $data['quantity_consumed'];
} else { // else add the data as new inv_no key
$newArray[$data['inv_no']] = $data;
}
}
echo "<pre>";
print_r($newArray);
输出:-
Array
(
[INV828] => Array
(
[org_name] => shams interior
[inv_no] => INV828
[productid] => 1556
[productname] => E001-4ft
[warehousename] => Paharganj
[date] => 2018-04-19 16:55:05
[quantity_consumed] => 312
[invoiceid] => 80364
)
[INV829] => Array
(
[org_name] => shams interior
[inv_no] => INV829
[productid] => 1556
[productname] => E001-4ft
[warehousename] => Paharganj
[date] => 2018-04-19 16:55:05
[quantity_consumed] => 512
[invoiceid] => 80364
)
)