Search code examples
phpmysqlcodeignitercodeigniter-3

php json array of arrays and get some key values from deep nested


I have a JSON data get from an API and I want to get/access for each AttributeValues the code and values to store into the tables, code as tables columns and values are dynamic for each record. I don't need to store the Attachements and Total from the json values. i want to store to tables as an insert into (ID, TrackingNumber, CustomerFull) values (json values). any codeigniter, or php soluton or hint will be much appricate.

 {
    "Values": [{
            "AttributeValues": [{
                    "ID": 0,
                    "Name": "ID",
                    "Values": [
                        "720"
                    ],
                    "DataType": "Number",
                    "Code": "ID",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Tracking Number",
                    "Values": [
                        "xxxx/1094/180730/1"
                    ],
                    "DataType": "String",
                    "Code": "TrackingNumber",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Customer Full",
                    "Values": [
                        "Ali Abdi"
                    ],
                    "DataType": "String",
                    "Code": "CustomerFull",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                }

            ],
            "Attachements": null
        },
        {
            "AttributeValues": [{
                    "ID": 0,
                    "Name": "ID",
                    "Values": [
                        "757"
                    ],
                    "DataType": "Number",
                    "Code": "ID",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Tracking Number",
                    "Values": [
                        "xxx/1094/180731/1"
                    ],
                    "DataType": "String",
                    "Code": "TrackingNumber",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Customer Full",
                    "Values": [
                        "Aberash Haile"
                    ],
                    "DataType": "String",
                    "Code": "CustomerFull",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Service Code",
                    "Values": [
                        "SO-1096"
                    ],
                    "DataType": "String",
                    "Code": "ServiceCode",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Request Date",
                    "Values": [
                        "7/31/2018 11:04:06 AM"
                    ],
                    "DataType": "Datetime",
                    "Code": "RequestDate",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                }
            ],
            "Attachements": null
        }
    ],
    "Total": 335
}

Solution

  • First of all, you should convert your JSON to array then use loop(foreach, for, while...) and get value accordingly like this:

    <?php 
    
     $json = '{
      "Values":[
        {
            "AttributeValues":[
                {
                "ID":0,
                "Name":"ID",
                "Values":[
                    "720"
                ],
                "DataType":"Number",
                "Code":"ID",
                "IsVisible":true,
                "IsValueEmptyOrNull":false,
                "IsDefault":true
                },
                {
                "ID":0,
                "Name":"Tracking Number",
                "Values":[
                    "xxxx/1094/180730/1"
                ],
                "DataType":"String",
                "Code":"TrackingNumber",
                "IsVisible":true,
                "IsValueEmptyOrNull":false,
                "IsDefault":true
                },
                {
                "ID":0,
                "Name":"Customer Full",
                "Values":[
                    "Ali Abdi"
                ],
                "DataType":"String",
                "Code":"CustomerFull",
                "IsVisible":true,
                "IsValueEmptyOrNull":false,
                "IsDefault":true
                }
            ],
            "Attachements":null
        },
        {
            "AttributeValues":[
                {
                "ID":0,
                "Name":"ID",
                "Values":[
                    "757"
                ],
                "DataType":"Number",
                "Code":"ID",
                "IsVisible":true,
                "IsValueEmptyOrNull":false,
                "IsDefault":true
                },
                {
                "ID":0,
                "Name":"Tracking Number",
                "Values":[
                    "xxx/1094/180731/1"
                ],
                "DataType":"String",
                "Code":"TrackingNumber",
                "IsVisible":true,
                "IsValueEmptyOrNull":false,
                "IsDefault":true
                },
                {
                "ID":0,
                "Name":"Customer Full",
                "Values":[
                    "Aberash Haile"
                ],
                "DataType":"String",
                "Code":"CustomerFull",
                "IsVisible":true,
                "IsValueEmptyOrNull":false,
                "IsDefault":true
                },
                {
                "ID":0,
                "Name":"Service Code",
                "Values":[
                    "SO-1096"
                ],
                "DataType":"String",
                "Code":"ServiceCode",
                "IsVisible":true,
                "IsValueEmptyOrNull":false,
                "IsDefault":true
                },
                {
                "ID":0,
                "Name":"Request Date",
                "Values":[
                    "7/31/2018 11:04:06 AM"
                ],
                "DataType":"Datetime",
                "Code":"RequestDate",
                "IsVisible":true,
                "IsValueEmptyOrNull":false,
                "IsDefault":true
                }
            ],
            "Attachements":null
        }
    ],
    "Total":335
    }';
    
    $json_data = json_decode($json, true);
    //echo "<pre>"; print_r($json_data['Values']); exit;
    if(isset($json_data['Values'])){
        foreach($json_data as $data){
            foreach($data as $attr){
                if(isset($attr['AttributeValues'])){
                    foreach($attr['AttributeValues'] as $value){
                        if(isset($value['Values'])){
                            foreach($value['Values'] as $val){
                                $attribute_values = $val;
                                echo "<pre>"; print_r($attribute_values); echo "</pre>";
                            }
                        } 
                    }
                }
            }
        }
    }
    ?>