php json_decode无法反对

时间:2015-04-13 03:45:28

标签: php arrays json

我在json对象中有一个字段我正在尝试进行模式匹配但是我似乎无法到达对象的第二层。

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

代码:

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;
    }
}

但是到达第二层数组的任何尝试都失败了;

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

或类似。

如何在上面的json查看器和Freetoo的模式匹配中访问字段'labels'? 这是我想要达到的确切对象。

2 个答案:

答案 0 :(得分:1)

不,一旦进入foreach,就没有更多的深度可以迭代了,它只是那之后的字符串(based on the dump),它不再可迭代,所以内部的foreach是无用的。只需访问字符串索引并创建所需条件:

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

如果您坚持在内部使用另一个多余的foreach,请指向当前批处理数组,而不是字符串。

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'];
        }

    }
}

答案 1 :(得分:0)

我希望这是你想要的...... 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;
     }
}