对数组的单列求和[重复]

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

我正在为 Shopify 创建一个运输服务应用程序,我的回调 URL 有这个 JSON 帖子,使用

json_decode
来创建数组。我想要求和的值是
grams
并放入变量中,然后使用
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"
   }
}
php arrays multidimensional-array sum
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

2
投票

首先,您的示例 JSON 项目之间没有逗号,所以我添加了它。

但是您可以使用

foreach
array_map
 来快速获取项目的总和,而不是使用 
array_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
© www.soinside.com 2019 - 2024. All rights reserved.