Parsing complex jason data in php

<?php

$json ='{
  "productsku": "",
  "name": "",
  "description": "",
  "short_description": "",
  "meta_description": "",
  "status": "",
  "tax_class_id": "",
  "price": "",
  "img": "",
  "img1": "",
  "img2": "",
  "img3": "",
  "img4": "",
  "img5": "",
  "category_id": "",
  "variant": [
    {
      "seqId": 3,
      "SKU": "",
      "attribute": [
        {
          "attribute_name": "size",
          "value": "l"
        },
        {
          "attribute_name": "color",
          "value": "red"
        }
      ]
    },
    {
      "seqId": 3,
      "SKU": "",
      "attribute": [
        {
          "attribute_name": "size",
          "value": "l"
        },
        {
          "attribute_name": "color",
          "value": "red"
        }
      ]
    },
    {
      "seqId": 3,
      "SKU": "",
      "attribute": [
        {
          "attribute_name": "size",
          "value": "l"
        },
        {
          "attribute_name": "color",
          "value": "red"
        }
      ]
    },
    {
      "seqId": 3,
      "SKU": "",
      "attribute": [
        {
          "attribute_name": "size",
          "value": "l"
        },
        {
          "attribute_name": "color",
          "value":    "red"
        }
      ]
    }
  ]
}';


$arr = json_decode($json);

foreach($arr as $parentkey=>$parentvalue){
     if(!is_array($parentvalue)){
                    echo $parentkey." " . $parentvalue;
      }
    
    if($parentkey == 'variant'){
        foreach($parentvalue as $variantArray){
            
            foreach($variantArray as $variantArrayKey=>$variantArrayValue){
                if(!is_array($variantArrayValue)){
                    echo $variantArrayKey." " . $variantArrayValue;
                }
                
               if($variantArrayKey == 'attribute'){
                foreach($variantArrayValue as $attribute){
                    foreach($attribute as $attributeKey=>$attributeValue){
                        echo $attributeKey."   " . $attributeValue;
                    }
                }
            }  
          }
            
       }
            
    }
} 



?>

Comments

Popular posts from this blog

What are sales territory in Netsuite?

How to get and set a sublist in netsuite suitlet suitescript 2.0