mine
mine

Reputation: 245

php json_decode can't get to object

I have a field in a json object I am trying to do a pattern match for but I cannot seem to reach the second layer of the object.

JSON:

{
    "1680488": {
        "SUBID": "1680488",
        "os": "FreeBSD 10 x64",
        "ram": "2048 MB",
        "disk": "Virtual 40 GB",
        "main_ip": "107.191.60.48",
        "vcpu_count": "2",
        "location": "Tokyo",
        "DCID": "25",
        "default_password": "4",
        "date_created": "2015-02-06 02:27:22",
        "pending_charges": 7.61,
        "status": "active",
        "cost_per_month": "18.00",
        "current_bandwidth_gb": 2.706,
        "allowed_bandwidth_gb": "600",
        "netmask_v4": "255.255.254.0",
        "gateway_v4": "107.191.60.1",
        "power_status": "running",
        "VPSPLANID": "8",
        "v6_network": "2001:19f0:7000:8945::",
        "v6_main_ip": "2001:19f0:7000:8945::64",
        "v6_network_size": "64",
        "label": "Freetoo",
        "internal_ip": "",
        "kvm_url": "https://my.vultr.com/subs/vps/novnc/api.php?data=",
        "auto_backups": "yes"
    },
    "1685960": {
        "SUBID": "1685960",
        "os": "FreeBSD 10 x64",
        "ram": "768 MB",
        "disk": "Virtual 15 GB",
        "main_ip": "108.61.190.64",
        "vcpu_count": "1",
        "location": "Frankfurt",
        "DCID": "9",
        "default_password": "1",
        "date_created": "2015-02-09 00:22:42",
        "pending_charges": 2.54,
        "status": "active",
        "cost_per_month": "6.00",
        "current_bandwidth_gb": 0.612,
        "allowed_bandwidth_gb": "1000",
        "netmask_v4": "255.255.255.0",
        "gateway_v4": "108.61.190.1",
        "power_status": "running",
        "VPSPLANID": "29",
        "v6_network": "2001:19f0:6c00:8141::",
        "v6_main_ip": "2001:19f0:6c00:8141::64",
        "v6_network_size": "64",
        "label": "",
        "internal_ip": "",
        "kvm_url": "https://my.vultr.com/subs/vps/novnc/api.php?data=",
        "auto_backups": "yes"
    },
    "1694100": {
        "SUBID": "1694100",
        "os": "FreeBSD 10 x64",
        "ram": "768 MB",
        "disk": "Virtual 15 GB",
        "main_ip": "108.61.175.20",
        "vcpu_count": "1",
        "location": "London",
        "DCID": "8",
        "default_password": "9",
        "date_created": "2015-02-12 13:21:33",
        "pending_charges": 2.54,
        "status": "active",
        "cost_per_month": "6.00",
        "current_bandwidth_gb": 1.51,
        "allowed_bandwidth_gb": "1000",
        "netmask_v4": "255.255.255.0",
        "gateway_v4": "108.61.175.1",
        "power_status": "running",
        "VPSPLANID": "29",
        "v6_network": "2001:19f0:7400:84c6::",
        "v6_main_ip": "2001:19f0:7400:84c6::64",
        "v6_network_size": "64",
        "label": "",
        "internal_ip": "",
        "kvm_url": "https://my.vultr.com/subs/vps/novnc/api.php?data=",
        "auto_backups": "yes"
    },
    "1847630": {
        "SUBID": "1847630",
        "os": "FreeBSD 10 x64",
        "ram": "768 MB",
        "disk": "Virtual 15 GB",
        "main_ip": "108.61.169.203",
        "vcpu_count": "1",
        "location": "Sydney",
        "DCID": "19",
        "default_password": "ye!5",
        "date_created": "2015-04-12 05:57:47",
        "pending_charges": 0.11,
        "status": "active",
        "cost_per_month": "5.00",
        "current_bandwidth_gb": 2.43,
        "allowed_bandwidth_gb": "200",
        "netmask_v4": "255.255.254.0",
        "gateway_v4": "108.61.168.1",
        "power_status": "stopped",
        "VPSPLANID": "31",
        "v6_network": "2001:19f0:5800:8561::",
        "v6_main_ip": "2001:19f0:5800:8561::64",
        "v6_network_size": "64",
        "label": "sydney temp",
        "internal_ip": "",
        "kvm_url": "https://my.vultr.com/subs/vps/novnc/api.php?data=",
        "auto_backups": "no"
    }
}

Code:

function getlist(){
    $list_output = file_get_contents('https://api.vultr.com/v1/server/list?api_key=UY');
    return $list_output;
}

//var_dump($server_output);

echo '<br><br><br><br>';
$output = getlist();
$decoded = json_decode($output, true);
//var_dump($decoded);

foreach ($decoded as $value) {
    //echo $value['SUBID'];
    foreach ($value['SUBID'] as $piece) {
        echo '<br>';
        //var_dump($value);
        echo $piece;
    }
}

but any attempt to reach that second layer array fails with;

Warning: Invalid argument supplied for foreach() in /home/comm/www/mkdomain/vultr-mkvps.php on line 14

or similar.

How do I access the field 'labels' n the json viewer above and pattern match for Freetoo? That is the exact object I am trying to reach.

Upvotes: 1

Views: 861

Answers (2)

Karthik K
Karthik K

Reputation: 35

I hope this is you want..sample code

$output = json_decode($jsoninput,true);
foreach($output as $subid=>$row){
     echo $subid;   
     foreach ($row as $piece) {
                echo '<br>';
                //var_dump($row);
                echo $piece;
     }
}

Upvotes: 0

Kevin
Kevin

Reputation: 41885

No, once inside the foreach there is no more depth to iterate, its just strings after that (based on the dump), its not iterable anymore, so that foreach inside is useless. Just access the strings indices and make your desired condition:

foreach($decoded as $value) {
    $id = $value['SUBID']; // string not array
    $label = $value['label']; // string
    if($label === 'Freetoo') {
        // do something
        echo $id; // and others that you have to do
    }
}

Sample Output

If you're insistent of having another superfluous foreach inside, point to the current batch array, not the string.

foreach($decoded as $value) {
    foreach($value as $key => $piece) { // point to `$value` the array, not `$value['SUBID']` (string)
        if($key === 'label' && $piece === 'Freetoo') {
            echo $value['SUBID'];
        }

    }
}

Upvotes: 1

Related Questions