我有2个不同的数组,它们的数组键相同,但值不同,试图将它们合并为1,但是这些值落在正确的键下,因此不确定如何实现。 这是我用于两个数组的PHP代码。
3.1
这遵循两个数组的结果
foreach($apiS->sku as $skuname=>$skuvalue) {
$skus1[$skuname] = array(
'promotion_price' => $skuvalue->price->priceText,
'quantity' => $skuvalue->quantity,
);
}
foreach($apiD->sku as $skuname=>$skuvalue) {
$skus2[$skuname] = array(
'price' => $skuvalue->price->priceText,
);
}
我的预期结果将是这样:
[skus1] => stdClass Object
(
[0] => stdClass Object
(
[promotion_price] => 69
[quantity] => 176
)
[3927138782588] => stdClass Object
(
[promotion_price] => 69
[quantity] => 176
)
)
[skus2] => stdClass Object
(
[0] => stdClass Object
(
[price] => 138.00
)
[3927138782588] => stdClass Object
(
[price] => 138.00
)
)
任何获得此想法的想法都将受到高度赞赏。谢谢
答案 0 :(得分:2)
如果要合并单个数组中的两个记录,请使用一个数组变量并按如下所示更改循环。
$skus = [];
foreach($apiS->sku as $skuname=>$skuvalue) {
$skus[$skuname]['promotion_price'] = $skuvalue->price->priceText;
$skus[$skuname]['quantity'] = $skuvalue->quantity;
}
foreach($apiD->sku as $skuname=>$skuvalue) {
$skus[$skuname]['price'] = $skuvalue->price->priceText;
}