求和数组值

时间:2014-06-08 05:30:05

标签: php arrays sum

我正在为Shopify制作一个送货服务应用程序,我的回调网址使用json_decode创建了一个数组的JSON帖子。我想要求和的值是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"
   }
}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

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

但是,您可以使用array_maparray_sum快速获取项目总和,而不是foreach循环。神奇的是在示例代码底部的这三个简单的行中;放在这里以便于审查:

// 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);