求数组值的总和

3

我正在为Shopify开发一个配送服务应用程序,我的回调URL有这个JSON post,使用json_decode将其转换成数组。我想汇总的值是,把它放入变量中,然后使用if ($weight <= $maxweight)与另一个变量的值进行比较。

{
   "rate": {
       "items": [
           {
               "name": "My Product 3",
               "sku": null,
               "quantity": 1,
               "grams": 1000,
               "price": 2000,
               "vendor": "TestVendor",
               "requires_shipping": true,
               "taxable": true,
               "fulfillment_service": "manual"
           },
           {
               "name": "My Product 1",
               "sku": null,
               "quantity": 1,
               "grams": 500,
               "price": 1000,
               "vendor": "TestVendor",
               "requires_shipping": true,
               "taxable": true,
               "fulfillment_service": "manual"
           },
           {
               "name": "My Product 2",
               "sku": null,
               "quantity": 1,
               "grams": 2000,
               "price": 3000,
               "vendor": "TestVendor",
               "requires_shipping": true,
               "taxable": true,
               "fulfillment_service": "manual"
           }
       ],
       "currency": "CAD"
   }
}
3个回答

2
你只需要一个简单的foreach循环,然后在循环内添加值即可。考虑以下示例:
$total_weight = 0;
$json_string = '{ "rate":{ "items":[ { "name":"My Product 3", "sku":null, "quantity":1, "grams":1000, "price":2000, "vendor":"TestVendor", "requires_shipping":true, "taxable":true, "fulfillment_service":"manual" }, { "name":"My Product 1", "sku":null, "quantity":1, "grams":500, "price":1000, "vendor":"TestVendor", "requires_shipping":true, "taxable":true, "fulfillment_service":"manual" }, { "name":"My Product 2", "sku":null, "quantity":1, "grams":2000, "price":3000, "vendor":"TestVendor", "requires_shipping":true, "taxable":true, "fulfillment_service":"manual" } ], "currency":"CAD" }}';
$json_data = json_decode($json_string, true);

$max_weight = 10000;
foreach($json_data['rate']['items'] as $key => $value)  {
    $total_weight += $value['grams'];
}

echo $total_weight; // 3500
// then just compare whatever $max_weight value has to $total_weight
// rest of your desired code

@Saul_dg 你可以使用foreach循环来实现,但是将array_maparray_sum耦合在一起可以更快地产生相同的结果。 - Giacomo1968

2

首先,你提供的JSON示例中没有逗号,我已经添加了。

但是,你不用使用foreach循环,可以使用array_maparray_sum来快速获取项目的总和。这三行代码是解决问题的关键,在示例代码底部方便查看:

// Convert the raw json to an array with json_encode & the 'true' option.
$json_array = json_decode($raw_json, true);

// Now use array_map to return and array of just the value of grams.
$grams_array = array_map(function ($item) { return intval($item['grams']); }, $json_array['rate']['items'] );

// Echo the sum of the grams array with by using array sum.
echo array_sum($grams_array);

这是完整的工作示例,其中包含您提供的JSON。
// Set the raw JSON.
$raw_json = <<<EOT
{
   "rate": {
       "items": [
           {
               "name": "My Product 3",
               "sku": null,
               "quantity": 1,
               "grams": 1000,
               "price": 2000,
               "vendor": "TestVendor",
               "requires_shipping": true,
               "taxable": true,
               "fulfillment_service": "manual"
           },
           {
               "name": "My Product 1",
               "sku": null,
               "quantity": 1,
               "grams": 500,
               "price": 1000,
               "vendor": "TestVendor",
               "requires_shipping": true,
               "taxable": true,
               "fulfillment_service": "manual"
           },
           {
               "name": "My Product 2",
               "sku": null,
               "quantity": 1,
               "grams": 2000,
               "price": 3000,
               "vendor": "TestVendor",
               "requires_shipping": true,
               "taxable": true,
               "fulfillment_service": "manual"
           }
       ],
       "currency": "CAD"
   }
}
EOT;

// Convert the raw json to an array with json_encode & the 'true' option.
$json_array = json_decode($raw_json, true);

// Now use array_map to return and array of just the value of grams.
$grams_array = array_map(function ($item) { return intval($item['grams']); }, $json_array['rate']['items'] );

// Echo the sum of the grams array with by using array sum.
echo array_sum($grams_array);

0
  1. 使用 array_column() 分离所需的数据列。
  2. 对返回的数组调用 array_sum()

代码:(演示)

var_export(
    array_sum(
        array_column(
            $array['rate']['items'],
            'grams'
        )
    )
);

输出:

3500

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接