如何从这个多维关联数组中单独提取此密钥


如何

从这个多维数组中提取"属性"数组的值?

$json ='[
      {
        "attributes": [
          {
            "attributeID": 3216,
            "attributeValue": "white",
            "skuImageUrl": "image.jpg"
          },
          {
            "attributeID": 450,
            "attributeValue": "S",
            "attributeDisplayName": "gray"
          }
        ],
        "cargoNumber": "",
        "amountOnSale": 446,
        "skuCode": "3740158232110",
        "skuId": 3740158232110,
        "specId": "94d1d179497744028aa76873afdeba62",
        "consignPrice": 18.5
      },
      {
        "attributes": [
          {
            "attributeID": 3216,
            "attributeValue": "Walking",
            "skuImageUrl": "ball.jpg",
            "attributeDisplayName": "waiting"
          },
          {
            "attributeID": 450,
            "attributeValue": "M",
            "attributeDisplayName": "stay"
          }
        ],
        "cargoNumber": "",
        "amountOnSale": 0,
        "skuCode": "3740158232111",
        "skuId": 3740158232111,
        "specId": "cef93beef156f1799e736c649f36efae",
        "consignPrice": 18.5
      }
]'

我尝试过使用 for 循环,但我感到困惑。决定尝试每个循环,但最终更加困惑。 拜托,我怎样才能做到这一点?我的意思是仅使用循环中的属性数组。

尝试:

$jsonD = json_decode($json);
var_dump($jsonD[0]->attributes);
array(2) {
  [0]=>
  object(stdClass)#1 (3) {
    ["attributeID"]=>
    int(3216)
    ["attributeValue"]=>
    string(5) "white"
    ["skuImageUrl"]=>
    string(9) "image.jpg"
  }
  [1]=>
  object(stdClass)#2 (3) {
    ["attributeID"]=>
    int(450)
    ["attributeValue"]=>
    string(1) "S"
    ["attributeDisplayName"]=>
    string(4) "gray"
  }
}
foreach($jsonD[0]->attributes as $attribute){
}

最新更新