这是我的两个多维数组,我想获得唯一的值,当出现相同的iProduct_id值时,则减去fTotal_qty。我需要帮助。谢谢你。
这是Array1
Array(
[0] => Array
(
[iProduct_id] => 1
[fTotal_qty] => 200
)
[1] => Array
(
[iProduct_id] => 4
[fTotal_qty] => 100
)
)
这是Array2
Array
(
[0] => Array
(
[iProduct_id] => 1
[fTotal_qty] => 500
)
[1] => Array
(
[iProduct_id] => 2
[fTotal_qty] => 400
)
[2] => Array
(
[iProduct_id] => 6
[fTotal_qty] => 700
)
[3] => Array
(
[iProduct_id] => 4
[fTotal_qty] => 300
)
[4] => Array
(
[iProduct_id] => 5
[fTotal_qty] => 200
)
)
我想要这样的输出
iProduct_id = 1, fTotal_qty = 300
iProduct_id = 2, fTotal_qty = 400
iProduct_id = 6, fTotal_qty = 700
iProduct_id = 4, fTotal_qty = 200
答案 0 :(得分:2)
您需要在php中使用array_map()函数
array_map(myfunction,array1,array2,array3...)
在这里,myfunction是包含用于对项目进行减法和合并的逻辑的方法。
array1,array2,array3是可能包含您的数据的数组的数量。
答案 1 :(得分:1)
您可以执行以下操作:
foreach($array1 as $index => $value) {
$key = array_search($value['iProduct_id'], $array2);
if($key) {
$newArray[$index]['iProduct_id'] = $value['iProduct_id'];
$newArray[$index]['fTotal_qty'] = $array2[$key]['fTotal_qty'] - $value['fTotal_qty'];
}
}
$finalArray = array_diff($array2,$newArray);
var_dump(array_merge($finalArray,$newArray));
我希望这会有所帮助。
答案 2 :(得分:0)
<?php
$one =
[
[
'id'=>1,
'amount' => 100
],
[
'id'=>2,
'amount' => 200
],
];
$two =
[
[
'id'=>1,
'amount' => 700
],
[
'id'=>2,
'amount' => 800
],
[
'id'=>3,
'amount' => 900
],
];
$one_id_amount = array_column($one, 'amount', 'id');
foreach($result = $two as $k => $v)
$result[$k]['amount'] -= $one_id_amount[$v['id']] ?? 0;
var_export($result);
输出:
array (
0 =>
array (
'id' => 1,
'amount' => 600,
),
1 =>
array (
'id' => 2,
'amount' => 600,
),
2 =>
array (
'id' => 3,
'amount' => 900,
),
)
答案 3 :(得分:0)
<?php
$arr1 = array(
array('iProduct_id' => 4,'fTotal_qty' => 100),
array('iProduct_id' => 2,'fTotal_qty' => 100)
);
$arr2 = array(
array('iProduct_id' => 1,'fTotal_qty' => 500),
array('iProduct_id' => 2,'fTotal_qty' => 450),
array('iProduct_id' => 6,'fTotal_qty' => 700),
array('iProduct_id' => 4,'fTotal_qty' => 300),
array('iProduct_id' => 5,'fTotal_qty' => 200)
);
echo "<pre>";
echo "array1 = "; print_r($arr1);
echo "array2 = "; print_r($arr2);
$key = array_column($arr1, 'iProduct_id');
$arraySub = array();
$sub = array();
for ($i=0; $i < count($arr2); $i++) {
if (array_search($arr2[$i]['iProduct_id'],$key) != '' || array_search($arr2[$i]['iProduct_id'],$key) == 0)
{
$arraySub['val1'] = array_search($arr2[$i]['iProduct_id'],$key);
$arraySub['val2'] = $i;
}
$sub[] = $arraySub;
}
foreach ($sub as $value) {
if ($arr1[$value['val1']]['iProduct_id'] != '' && $arr2[$value['val2']]['iProduct_id'] != '') {
if ($arr1[$value['val1']]['iProduct_id'] == $arr2[$value['val2']]['iProduct_id'] ) {
$arr2[$value['val2']]['fTotal_qty'] = abs($arr2[$value['val2']]['fTotal_qty']) - abs($arr1[$value['val1']]['fTotal_qty']);
}
}
}
echo "(array1 - array2) = "; print_r($arr2); // your subtrected value has been stored in arr2
?>
答案 4 :(得分:0)
已测试答案:
<?php
$Array1 =
[
[
'iProduct_id'=>1,
'fTotal_qty' => 200
],
[
'iProduct_id'=>4,
'fTotal_qty' => 100
],
];
$Array2 =
[
[
'iProduct_id'=>1,
'fTotal_qty' => 500
],
[
'iProduct_id'=>2,
'fTotal_qty' => 400
],
[
'iProduct_id'=>6,
'fTotal_qty' => 700
],
[
'iProduct_id'=>4,
'fTotal_qty' => 300
],
[
'iProduct_id'=>5,
'fTotal_qty' => 200
],
];
$one_id_amount = array_column($Array1, 'fTotal_qty', 'iProduct_id');
foreach($result = $Array2 as $k => $v)
$result[$k]['fTotal_qty'] -= $one_id_amount[$v['iProduct_id']] ?? 0;
foreach ($result as $line) {
echo "iProduct_id =" . $line['iProduct_id'] . ", fTotal_qty = " . $line['fTotal_qty'] . "<br>";
}
我希望这会有所帮助。