'Sum array values
I’m making a shipping service app to Shopify and my callback URL have this JSON post using json_decode
to make an array. The value I want to sum is grams
and putting into variable and then compare with the value of another variable using 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"
}
}
Solution 1:[1]
You just need a simple foreach then under the loop, just add the value. Consider this example:
$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
Solution 2:[2]
First, your example JSON didn’t have commas between items, so I added that.
But instead of a foreach
loop, you can use array_map
and array_sum
to quickly get a sum of items. The magic is in these three simple lines at the bottom of the example code; placing here for easy review:
// 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);
And here is the full working example with the JSON as you presented it.
// 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);
Solution 3:[3]
- Isolate the desired column of data with
array_column()
. - Call
array_sum()
on that returned array.
Code: (Demo)
var_export(
array_sum(
array_column(
$array['rate']['items'],
'grams'
)
)
);
Output:
3500
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user1978142 |
Solution 2 | |
Solution 3 | mickmackusa |