如何在 php 中获取 json 键值?

how to get json key value in php?

如何使用 PHP 获取此 JSON 中的所有键值? 我的 php 代码是:

<?php 
$json = json_decode('{
    "data": {
        "after": [{
            "google.com": "35"
        }, {
            "yahoo.com": "10"
        }, {
            "worldi.ir": "30"
        }, {
            "cnn.com": "554"
        }, {
            "scio.ir": "887"
        }],
        "before": [{
            "bbc.com": "44"
        }, {
            "rtl.com": "15"
        }, {
            "quran.com": "9"
        }, {
            "lap.com": "12"
        }, {
            "search.com": "13"
        }]
    }
}');
foreach($json->data->after as $key => $value) {
    echo "$key<br/>";
    foreach(((array)$json->data->after)[$key] as $val) {
        echo "$val<br/>";
    }
}
?>

结果

0
35
1
10
2
30
3
554
4
887

不显示键值。我想获取所有密钥 value.such 作为 google.com、yahoo.com、worldi.ir cnn.com 和 ...

after 是一个数组,因此在外层 foreach 循环中返回的 $key 只是一个索引(整数)。您应该在第二个 foreach 中再次包含 $key => $value 以获取每个内部对象的键。此外,您可以只在第一个 foreach 的 $value 上使用 foreach。您不必再次指定整个密钥路径。

<?php 
$json = json_decode('{
    "data": {
        "after": [{
            "google.com": "35"
        }, {
            "yahoo.com": "10"
        }, {
            "worldi.ir": "30"
        }, {
            "cnn.com": "554"
        }, {
            "scio.ir": "887"
        }],
        "before": [{
            "bbc.com": "44"
        }, {
            "rtl.com": "15"
        }, {
            "quran.com": "9"
        }, {
            "lap.com": "12"
        }, {
            "search.com": "13"
        }]
    }
}');
foreach($json->data->after as $key => $value) {
    foreach($value as $k => $val) {
        echo "$k<br/>$val<br/>";
    }
}
?>

试试这个解决方案:

<?php
    $json = json_decode('{
        "data": {
            "after": [{
                "google.com": "35"
            }, {
                "yahoo.com": "10"
            }, {
                "worldi.ir": "30"
            }, {
                "cnn.com": "554"
            }, {
                "scio.ir": "887"
            }],
            "before": [{
                "bbc.com": "44"
            }, {
                "rtl.com": "15"
            }, {
                "quran.com": "9"
            }, {
                "lap.com": "12"
            }, {
                "search.com": "13"
            }]
        }
    }');


    foreach($json->data->after as $key => $value) {

        echo key($json->data->after[$key]);
        
        foreach(((array)$json->data->after)[$key] as $val) {
            echo "$val<br/>";
        }
    }

试试这个:

<?php 
$json = json_decode('{
    "data": {
        "after": [{
            "google.com": "35"
        }, {
            "yahoo.com": "10"
        }, {
            "worldi.ir": "30"
        }, {
            "cnn.com": "554"
        }, {
            "scio.ir": "887"
        }],
        "before": [{
            "bbc.com": "44"
        }, {
            "rtl.com": "15"
        }, {
            "quran.com": "9"
        }, {
            "lap.com": "12"
        }, {
            "search.com": "13"
        }]
    }
}');
foreach($json->data as $key1 => $value1) {
    echo "$key1: \n";
    foreach($value1 as $key2 => $value2) {
         echo "$key2 -> ";
         foreach($value2 as $key3 => $value3) {
             echo "$key3 -> $value3\n";
        }
    }
}
?>

输出:

after: 
0 -> google.com -> 35
1 -> yahoo.com -> 10
2 -> worldi.ir -> 30
3 -> cnn.com -> 554
4 -> scio.ir -> 887
before: 
0 -> bbc.com -> 44
1 -> rtl.com -> 15
2 -> quran.com -> 9
3 -> lap.com -> 12
4 -> search.com -> 13