合并 php 中的两个 stdClass 对象数组
Merge two stdClass object array in php
我有以下两个来自循环的 stdClass 数组。现在我需要在键 'id' 匹配时合并它们。
array (size=1)
0 =>
object(stdClass)[28]
public 'id' => string '78' (length=2)
public 'quantity' => string '5' (length=1)
array (size=1)
1 =>
object(stdClass)[31]
public 'product_id' => string '78' (length=2)
public 'quantity' => string '1' (length=1)
所以最后的数组变成了
array (size=1)
1 =>
object(stdClass)[31]
public 'product_id' => string '78' (length=2)
public 'quantity' => string '6' (length=1)
关于如何做到这一点的任何帮助?我使用 json_decode 从 [{"id":"78","quantity":"1"}]
这种格式的数据中解码原始数据。
如果您向 json_decode
添加一个额外的参数,您可以获得关联数组形式的数据,这更易于使用。我已经做了几个版本(第一个是 PHP 7 ),选择一个适合你系统的版本。
<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
$arr1 = json_decode('[{"id":"78","quantity":"1"}, {"id":"79","quantity":"3"}]', true);
$arr2 = json_decode('[{"id":"78","quantity":"5"}]', true);
$arr3 = array_merge($arr1, $arr2);
// V7
$result = [];
foreach ( $arr3 as $element ) {
$result[$element['id']] = ($result[$element['id']]??0)
+ $element['quantity'];
}
print_r($result);
// Prior to V7
$result = array();
foreach ( $arr3 as $element ) {
if ( !isset($result[$element['id']]) ){
$result[$element['id']] = 0;
}
$result[$element['id']] += $element['quantity'];
}
print_r($result);
我添加了另一个元素来展示它是如何相加的,但是它的输出是...
Array
(
[78] => 6
[79] => 3
)
Array
(
[78] => 6
[79] => 3
)
这是一个维护原始问题格式的解决方案。
它还使用 array_reduce 这是一种处理数组的简洁方法。
<?php
$input1 = '[{"id":"78","quantity":"7800"},
{"id":"79","quantity":"7900"},
{"id":"80","quantity":"8000"}]';
$input2 = '[{"id":"78","quantity":"6"},
{"id":"79","quantity":"8"},
{"id":"80","quantity":"6"},
{"id":"81","quantity":"7"}]';
$input1Arr = json_decode($input1);
$input2Arr = json_decode($input2);
$combinedArrays = array_merge($input1Arr, $input2Arr);
echo "combinedArrays = " . print_r($combinedArrays, true) . "\n";
$result = array_reduce($combinedArrays,
function($intermediateResult, $item){
if ( ! array_key_exists($item->id, $intermediateResult) ) {
// First time encountering an object with this id
$intermediateResult[$item->id] = $item;
}
else {
// We have an object with this id already so just add the quantity
$intermediateResult[$item->id]->quantity += $item->quantity;
}
return $intermediateResult;
}, []);
// Get the values from the result array
print_r(array_values($result));
我有以下两个来自循环的 stdClass 数组。现在我需要在键 'id' 匹配时合并它们。
array (size=1)
0 =>
object(stdClass)[28]
public 'id' => string '78' (length=2)
public 'quantity' => string '5' (length=1)
array (size=1)
1 =>
object(stdClass)[31]
public 'product_id' => string '78' (length=2)
public 'quantity' => string '1' (length=1)
所以最后的数组变成了
array (size=1)
1 =>
object(stdClass)[31]
public 'product_id' => string '78' (length=2)
public 'quantity' => string '6' (length=1)
关于如何做到这一点的任何帮助?我使用 json_decode 从 [{"id":"78","quantity":"1"}]
这种格式的数据中解码原始数据。
如果您向 json_decode
添加一个额外的参数,您可以获得关联数组形式的数据,这更易于使用。我已经做了几个版本(第一个是 PHP 7 ),选择一个适合你系统的版本。
<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
$arr1 = json_decode('[{"id":"78","quantity":"1"}, {"id":"79","quantity":"3"}]', true);
$arr2 = json_decode('[{"id":"78","quantity":"5"}]', true);
$arr3 = array_merge($arr1, $arr2);
// V7
$result = [];
foreach ( $arr3 as $element ) {
$result[$element['id']] = ($result[$element['id']]??0)
+ $element['quantity'];
}
print_r($result);
// Prior to V7
$result = array();
foreach ( $arr3 as $element ) {
if ( !isset($result[$element['id']]) ){
$result[$element['id']] = 0;
}
$result[$element['id']] += $element['quantity'];
}
print_r($result);
我添加了另一个元素来展示它是如何相加的,但是它的输出是...
Array
(
[78] => 6
[79] => 3
)
Array
(
[78] => 6
[79] => 3
)
这是一个维护原始问题格式的解决方案。 它还使用 array_reduce 这是一种处理数组的简洁方法。
<?php
$input1 = '[{"id":"78","quantity":"7800"},
{"id":"79","quantity":"7900"},
{"id":"80","quantity":"8000"}]';
$input2 = '[{"id":"78","quantity":"6"},
{"id":"79","quantity":"8"},
{"id":"80","quantity":"6"},
{"id":"81","quantity":"7"}]';
$input1Arr = json_decode($input1);
$input2Arr = json_decode($input2);
$combinedArrays = array_merge($input1Arr, $input2Arr);
echo "combinedArrays = " . print_r($combinedArrays, true) . "\n";
$result = array_reduce($combinedArrays,
function($intermediateResult, $item){
if ( ! array_key_exists($item->id, $intermediateResult) ) {
// First time encountering an object with this id
$intermediateResult[$item->id] = $item;
}
else {
// We have an object with this id already so just add the quantity
$intermediateResult[$item->id]->quantity += $item->quantity;
}
return $intermediateResult;
}, []);
// Get the values from the result array
print_r(array_values($result));