仅从数组中的第一级获取特定键

时间:2014-11-12 00:28:47

标签: php arrays json

我试图从我的数组中的特定键获取值。但是,密钥(名称)存在于2个级别,这不是我可以控制的。

我需要在多维数组中检查数组中键'totalWeight'的值。

我正在接收数组并像这样做我的支票....

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

if($body['eventName'] == 'shippingrates.fetch') {
  $shippingWeight = $body['content']['totalWeight'];
  // other logic code here...
}

其余的代码工作正常,问题是'totalWeight'存在于2个级别,我只需要第一级(这是购物车的总重量,而不是购物车中的单个商品...... )

我收到的数组如下:

{
  "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
  }
}

这就是我正在检查......

if($shippingWeight > '3000') {
  $rate = $shippingRatesHigh; 
} elseif($shippingWeight > '5000') {
  $rate = $shippingRatesMax;
} else {
  // default
  $rate = $shippingRatesStd;
}

当我在购物车中有多个商品时,它会将'totalWeight'基于['items']数组中的一个条目,如果只有一个产品存在则我的支票有效...

如果“内容”内部只有'totalWeight'而不进一步进入'items'中的数组,我该怎么办呢。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

一开始,你收到的json数据是一个对象,即它包裹在{}而不是一个包含在[]中的数组,所以为什么要强制它进入一个数组。

如果你使用这个想法,我认为它更容易想象。

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

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

如果您想获得单个物品重量或例如检查所有物品重量是否等于总重量,您可以

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

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

我一直认为对象比数组更容易,更清晰。

我测试了代码,这是使用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;

输出

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

我认为这就是你要求它做的事。