php 从 JSON 输入中检索数组键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10914687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:20:19  来源:igfitidea点击:

Retrieving array keys from JSON input

phparraysjsonforeach

提问by Nick

I have this array:

我有这个数组:

$json = json_decode('
{"entries":[
{"id": "29","name":"John", "age":"36"},
{"id": "30","name":"Hyman", "age":"23"}
]}
');

and I am looking for a PHP "for each" loop that would retrieve the key names under entries, i.e.:

我正在寻找一个 PHP“for each”循环,它可以检索 下的键名entries,即:

id
name
age

How can I do this?

我怎样才能做到这一点?

回答by Sena

Try it

尝试一下

foreach($json->entries as $row) {
    foreach($row as $key => $val) {
        echo $key . ': ' . $val;
        echo '<br>';
    }
}

In the $key you shall get the key names and in the val you shal get the values

在 $key 中,您将获得键名,而在 val 中,您将获得值

回答by Kar.ma

I was not satisfied with other answers so I add my own. I believe the most general approach is:

我对其他答案不满意,所以我添加了我自己的答案。我认为最通用的方法是:

$array = get_object_vars($json->entries[0]);
foreach($array as $key => $value) {
  echo $key . "<br>";
}

where I used entries[0]because you assume that all the elements of the entriesarray have the same keys.

我使用的地方entries[0]是因为您假设entries数组的所有元素都具有相同的键。

Have a look at the official documentation for key: http://php.net/manual/en/function.key.php

看看官方文档keyhttp: //php.net/manual/en/function.key.php

回答by Emmanuel Okeke

You could do something like this:

你可以这样做:

foreach($json->entries as $record){
    echo $record->id;
    echo $record->name;
    echo $record->age;
}

If you pass trueas the value for the second parameter in the json_decodefunction, you'll be able to use the decoded value as an array.

如果您true作为json_decode函数中第二个参数的值传递,您将能够将解码后的值用作数组。

回答by Inamur Rahman

    $column_name =[];
    foreach($data as $i){
        foreach($i as $key => $i){
            array_push($column_name, $key);
        }
        break;
    }

回答by gingerCodeNinja

Alternative answer using arrays rather than objects - passing true to json_decodewill return an array.

使用数组而不是对象的替代答案 - 传递 truejson_decode将返回一个数组。

$json = '{"entries":[{"id": "29","name":"John", "age":"36"},{"id": "30","name":"Hyman", "age":"23"}]}';
$data = json_decode($json, true);
$entries = $data['entries'];

foreach ($entries as $entry) {
    $id = $entry['id'];
    $name = $entry['name'];
    $age = $entry['age'];
    printf('%s (ID %d) is %d years old'.PHP_EOL, $name, $id, $age);
}

Tested at https://www.tehplayground.com/17zKeQcNUbFwuRjC

https://www.tehplayground.com/17zKeQcNUbFwuRjC测试

回答by Mihai Stancu

foreach($json->entries[0] AS $key => $name) {
    echo $key;
}

回答by karthik

You could try getting the properties of the object using get_object_vars:

您可以尝试使用以下方法获取对象的属性get_object_vars

 $keys = array();

 foreach($json->entries as $entry)
   $keys += array_keys(get_object_vars($entry));

 print_r($keys);