合并两个多维数组并在其他两列的组合出现多次时添加列值

问题描述 投票:0回答:2

我正在尝试为一个网站编写一个购物清单应用程序,该网站已经以 JSON 列出了成分。

完整代码在底部,但只是为了解释;我的目标是能够合并两个数组,当“名称”(最好还有“度量”)匹配时添加“数量”中的值。

例如两个 JSON feed 中均列出了“葵花籽油”。 我希望它输出为:

{"name":"Sunflower oil","quantity":110,"measure":"ml"}

但它当前覆盖了 JSON 并且输出是:

{"name":"Sunflower oil","quantity":10,"measure":"ml"},

这是我的代码:

<?php
$a = '{"ingredients":[{"name": "Sunflower oil","quantity": 100,"measure": "ml"}]}';
$b = '{"ingredients":[{"name": "Sunflower oil","quantity": 10,"measure": "ml"},{"name": "Fennel seeds","quantity": 1,"measure": "teaspoon"},{"name": "Garlic","quantity": 1,"measure": "clove"}]}';
print_r( json_encode(array_merge(json_decode($a, true),json_decode($b, true))) );
?>

所需输出:

{
    "ingredients": [
        {
            "name": "Sunflower oil",
            "quantity": 110,
            "measure": "ml"
        },
        {
            "name": "Olive oil",
            "quantity": 50,
            "measure": "ml"
        },
        {
            "name": "Fennel seeds",
            "quantity": 1,
            "measure": "teaspoon"
        },
        {
            "name": "Garlic",
            "quantity": 1,
            "measure": "clove"
        }
    ]
}
php arrays sum grouping array-merge
2个回答
1
投票

您可以使用以下代码来获得预期的结果:

<?php
$a = '{"ingredients":[{"name": "Sunflower oil","quantity": 100,"measure": "ml"},{"name": "Olive oil","quantity": 50,"measure": "ml"}]}';
$b = '{"ingredients":[{"name": "Sunflower oil","quantity": 10,"measure": "ml"},{"name": "Fennel seeds","quantity": 1,"measure": "teaspoon"},{"name": "Garlic","quantity": 1,"measure": "clove"}]}';

$aArr = json_decode($a, true);
$bArr = json_decode($b, true);
$sumArr = array("ingredients" => array());

foreach ($aArr['ingredients'] as $valA) {
    foreach ($bArr['ingredients'] as $keyB => $valB) {
        if ($valA['name'] == $valB['name'] && 
            $valA['measure'] == $valB['measure']) {

            $valA['quantity'] += $valB['quantity'];
            $sumArr['ingredients'][] = $valA;

            unset($bArr['ingredients'][$keyB]);
            continue 2;
        } 
    }
    $sumArr['ingredients'][] = $valA;
}
$sumArr['ingredients'] = array_merge($sumArr['ingredients'], $bArr['ingredients']);
print_r( json_encode( $sumArr ));
?>

0
投票
  1. 解码两个 json 字符串并访问它们的
    ingredients
  2. 合并两个二维数组。
  3. 循环查看项目。
  4. 根据两个标识列值定义分组字符串。
  5. 如果第一次遇到分组字符串,则创建一个新的引用变量并将其推入结果数组中。
  6. 如果不是第一次遇到分组字符串,请将项目的数量值添加到相关参考的数量中。
  7. 循环后,创建一个数组,其中包含与结果数组相关的成分键,然后重新 json 编码。

代码:(演示

$ingred1 = json_decode($a, true)['ingredients'];
$ingred2 = json_decode($b, true)['ingredients'];

$result = [];
foreach (array_merge($ingred1, $ingred2) as $item) {
    $groupBy = $item['name'] . '_' . $item['measure'];
    if (!isset($ref[$groupBy])) {
        $ref[$groupBy] = $item;
        $result[] =& $ref[$groupBy];
        continue;
    }
    $ref[$groupBy]['quantity'] += $item['quantity'];
};

echo json_encode(['ingredients' => $result], JSON_PRETTY_PRINT);
© www.soinside.com 2019 - 2024. All rights reserved.