PHP 将多维数组分成两部分,比较另一个数组
PHP divide multidimensional array into two part with comparing another array
我有两个多维数组如下:
商品新价格的数组 01:
Array
(
[42] => Array
(
[0] => 110
[1] => 0.00
)
[41] => Array
(
[0] => 80
[1] => 70.00
)
[43] => Array
(
[0] => 70
[1] => 60
)
[40] => Array
(
[0] => 90
[1] => 80
)
)
1
项目旧价格的数组 02:
Array
(
[42] => Array
(
[sales_price] => 100.00
[our_price] => 0.00
)
[41] => Array
(
[sales_price] => 80.00
[our_price] => 0.00
)
)
1
两个数组的数组键都是item ids (42,41,43,40)。
现在我需要比较 $new 和 $old 数组来创建更新和插入查询。
如果新数组中的值与旧数组中的值不同,则应更新table。如果新数组中的元素多于旧数组中的元素,则应为插入查询识别它们。
所以基本上我想将新数组分成两部分,比较旧数组并考虑上述条款。
期望两个数组如下:
Array
(
[42] => Array
(
[0] => 110
[1] => 0.00
)
[41] => Array
(
[0] => 80
[1] => 70.00
)
)
1
Array
(
[43] => Array
(
[0] => 70
[1] => 60
)
[40] => Array
(
[0] => 90
[1] => 80
)
)
1
这是我目前的代码:
foreach ($priceNew as $newIds => $newPrices) {
foreach($priceOld as $oldIds => $oldPrices) {
}
}
谁能帮我划分我的新阵列?
我想我理解你所说的足以为 PHP >8...
制作这个
$priceNew = [
42 => [ 110, 0.00 ],
41 => [ 80, 70.00 ],
43 => [ 70, 60 ],
40 => [ 90, 80 ],
];
$priceOld = [
42 => [
'sales_price' => 100.00,
'our_price' => 0.00
],
41 => [
'sales_price' => 80.00,
'our_price' => 0.00
],
];
$updateItems = [];
$insertItems = [];
foreach( $priceNew as $id => $values ) {
$old = $priceOld[ $id ] ?? null;
if ( !$old ) {
$insertItems[ $id ] = $values;
} else {
// Not sure if this is the correct conditions, might need to change.
if ( $values[ 0 ] !== $old[ 'sales_price' ] || $values[ 1 ] !== $old[ 'our_price' ] ) {
$updateItems[ $id ] = $values;
}
}
}
此数据已更改,新的键为 41 的元素与旧的相匹配。
$priceNew = [
42 => [ 110, 0.00 ],
41 => [ 80, 70.00 ],
43 => [ 70, 60 ],
40 => [ 90, 80 ],
];
$priceOld = [
42 => [
'sales_price' => 100.00,
'our_price' => 0.00
],
41 => [
'sales_price' => 80.00,
'our_price' => 70.00
],
];
你只需要比较键。这可以通过内部 PHP 函数 array_intersect_key and array_diff_key 来完成。更新数组仍然被过滤以满足特殊条件。
$updateArr = array_intersect_key($priceNew, $priceOld);
$filter = function($v,$k) use($priceOld){
return array_values($priceOld[$k]) != $v;
};
$updateArr = array_filter($updateArr, $filter, ARRAY_FILTER_USE_BOTH );
$otherArr = array_diff_key($priceNew,$updateArr); //added or remained
我有两个多维数组如下:
商品新价格的数组 01:
Array
(
[42] => Array
(
[0] => 110
[1] => 0.00
)
[41] => Array
(
[0] => 80
[1] => 70.00
)
[43] => Array
(
[0] => 70
[1] => 60
)
[40] => Array
(
[0] => 90
[1] => 80
)
)
1
项目旧价格的数组 02:
Array
(
[42] => Array
(
[sales_price] => 100.00
[our_price] => 0.00
)
[41] => Array
(
[sales_price] => 80.00
[our_price] => 0.00
)
)
1
两个数组的数组键都是item ids (42,41,43,40)。 现在我需要比较 $new 和 $old 数组来创建更新和插入查询。
如果新数组中的值与旧数组中的值不同,则应更新table。如果新数组中的元素多于旧数组中的元素,则应为插入查询识别它们。
所以基本上我想将新数组分成两部分,比较旧数组并考虑上述条款。
期望两个数组如下:
Array
(
[42] => Array
(
[0] => 110
[1] => 0.00
)
[41] => Array
(
[0] => 80
[1] => 70.00
)
)
1
Array
(
[43] => Array
(
[0] => 70
[1] => 60
)
[40] => Array
(
[0] => 90
[1] => 80
)
)
1
这是我目前的代码:
foreach ($priceNew as $newIds => $newPrices) {
foreach($priceOld as $oldIds => $oldPrices) {
}
}
谁能帮我划分我的新阵列?
我想我理解你所说的足以为 PHP >8...
制作这个$priceNew = [
42 => [ 110, 0.00 ],
41 => [ 80, 70.00 ],
43 => [ 70, 60 ],
40 => [ 90, 80 ],
];
$priceOld = [
42 => [
'sales_price' => 100.00,
'our_price' => 0.00
],
41 => [
'sales_price' => 80.00,
'our_price' => 0.00
],
];
$updateItems = [];
$insertItems = [];
foreach( $priceNew as $id => $values ) {
$old = $priceOld[ $id ] ?? null;
if ( !$old ) {
$insertItems[ $id ] = $values;
} else {
// Not sure if this is the correct conditions, might need to change.
if ( $values[ 0 ] !== $old[ 'sales_price' ] || $values[ 1 ] !== $old[ 'our_price' ] ) {
$updateItems[ $id ] = $values;
}
}
}
此数据已更改,新的键为 41 的元素与旧的相匹配。
$priceNew = [
42 => [ 110, 0.00 ],
41 => [ 80, 70.00 ],
43 => [ 70, 60 ],
40 => [ 90, 80 ],
];
$priceOld = [
42 => [
'sales_price' => 100.00,
'our_price' => 0.00
],
41 => [
'sales_price' => 80.00,
'our_price' => 70.00
],
];
你只需要比较键。这可以通过内部 PHP 函数 array_intersect_key and array_diff_key 来完成。更新数组仍然被过滤以满足特殊条件。
$updateArr = array_intersect_key($priceNew, $priceOld);
$filter = function($v,$k) use($priceOld){
return array_values($priceOld[$k]) != $v;
};
$updateArr = array_filter($updateArr, $filter, ARRAY_FILTER_USE_BOTH );
$otherArr = array_diff_key($priceNew,$updateArr); //added or remained