问题描述:

Im trying to get a value from a specific key in my array. However, the key(name) exists at 2 levels, and this is not something I have control of.

I need to check the array for the value of key 'totalWeight' in a multidimensional array.

Im receiving the array and doing my check like this....

$json = file_get_contents('php://input');

$body = json_decode($json, true);

if($body['eventName'] == 'shippingrates.fetch') {

$shippingWeight = $body['content']['totalWeight'];

// other logic code here...

}

The rest of the code works fine, the problem is that 'totalWeight' exists at 2 levels, and I need ONLY the first level (which is the total weight of the cart, not the individual items in the cart...)

The array I receive looks like this:

{

"eventName": "shippingrates.fetch",

"mode": "Live",

"createdOn": "2014-11-11T23:47:00.6132556Z",

"content": {

"items": [

{

"totalWeight": 1000,

"customFields": [

{

"name": "Gift",

"operation": null,

"type": "checkbox",

"options": "true|false",

"required": false,

"value": "false"

}

],

"unitPrice": 180

},

{

"totalWeight": 1200,

"customFields": [

{

"name": "Gift",

"operation": null,

"type": "checkbox",

"options": "true|false",

"required": false,

"value": "false"

}

],

"unitPrice": 200

}

],

"totalWeight": 9600,

"customFields": [

{

"name": "I have read and accept the conditions of sale",

"operation": null,

"type": "checkbox",

"options": "true|false",

"required": true,

"value": "true"

}

],

"itemsCount": 5,

"metadata": null

}

}

This is how I'm checking....

if($shippingWeight > '3000') {

$rate = $shippingRatesHigh;

} elseif($shippingWeight > '5000') {

$rate = $shippingRatesMax;

} else {

// default

$rate = $shippingRatesStd;

}

When I have multiple items in the cart, it bases the 'totalWeight' on one of the entries inside the ['items'] array, if only one product exists then my check works...

How can I get only that 'totalWeight' inside 'content' without it travelling further into the array inside 'items'.

Any help would be most appreciated!

网友答案:

Well for a start the json data you are receiving is an object i.e. its wrapped in {} and not an array which would be wrapped in [] so why force it into an array.

If you use this idea, I think its easier to visualize.

$json = file_get_contents('php://input');
$body = json_decode($json);

echo 'The total weight of all shipments is ' . $body->content->totalWeight;

If you then want to get an individual item weights or for example check that all the item weights are equal to the total weight, you could do

$sumOfItemRates = 0;
foreach( $body->content->items as $item) {
    $sumOfItemRates += $item->totalWeight;
}

echo 'Sum Of Individual Items Weights is ' . $sumOfItemRates;

I always think that objects are easier and cleaner to code than arrays.

I tested that code, here is the full test run using PHP CLI:

<?php
$json = '{
  "eventName": "shippingrates.fetch",
  "mode": "Live",
  "createdOn": "2014-11-11T23:47:00.6132556Z",
  "content": {
    "items": [
      {
        "totalWeight": 1000,
        "customFields": [
          {
            "name": "Gift",
            "operation": null,
            "type": "checkbox",
            "options": "true|false",
            "required": false,
            "value": "false"
          }
        ],
        "unitPrice": 180
      },
      {
        "totalWeight": 1200,
        "customFields": [
          {
            "name": "Gift",
            "operation": null,
            "type": "checkbox",
            "options": "true|false",
            "required": false,
            "value": "false"
          }
        ],
        "unitPrice": 200
      }
    ],
    "totalWeight": 9600,
    "customFields": [
      {
        "name": "I have read and accept the   conditions of sale",
        "operation": null,
        "type": "checkbox",
        "options": "true|false",
        "required": true,
        "value": "true"
      }
    ],

    "itemsCount": 5,
    "metadata": null
  }
}';

$body = json_decode($json);

echo 'The total weight of all shipments is ' . $body->content->totalWeight . PHP_EOL;

$sumOfItemRates = 0;
foreach( $body->content->items as $item) {
    $sumOfItemRates += $item->totalWeight;
}

echo 'Sum Of Individual Items Weights is ' . $sumOfItemRates;

And the output was

The total weight of all shipments is 9600
Sum Of Individual Items Weights is 2200

Which I believe is what you were asking it to do.

相关阅读:
Top