按ID获取JSON组名称,而不是值

时间:2022-07-26 20:35:21

I have a valid JSON file like this:

我有一个有效的JSON文件,如下所示:

{
    "default": {
        "key_value": "Default Value"
    },
    "test": {
        "key_value": "Test Value"
    }
}

I want to get the group names as a string: "default" and "test". I know that i could use something like:

我想将组名称作为字符串:“default”和“test”。我知道我可以使用类似的东西:

{
    "general": {
        "id": "default",
        "key_value": "Default Value"
    },
    {
        "id": "test",
        "key_value": "Test Value"
    }
}

But since my JSON is valid and my functions for reading and saving the file are already working fine, i want to achieve this with the original structure.

但由于我的JSON是有效的,我的阅读和保存文件的功能已经正常工作,我希望用原始结构实现这一点。

For experimenting I got something like:

为了实验,我有类似的东西:

$json = file_get_contents("file.json");
$json_data = json_decode($json, true); // I guess assoc must be true?
foreach ($json_data as $item) } // My hope was that i am able to read $item, however it is an array
    echo $item["key_value"]."<br />"; // That gives me "Default Value" and "Test Value", so thats at least something
}

I guess "default" and "test" are within the first array of the json? Now i need to find a way to read them and save them as a string. A foreach couldn't be that wrong since if a group like "test2" is added, i want to read it aswell.

我猜“默认”和“测试”都在json的第一个数组内?现在我需要找到一种方法来读取它们并将它们保存为字符串。 foreach不可能是错误的,因为如果添加像“test2”这样的组,我也想读它。

I am struggling with undefined and illegal offsets (since i cant use arrays as keys?).

我正在努力解决未定义和非法的偏移(因为我不能使用数组作为键?)。

Since I am getting the values with something like:

因为我得到的值类似于:

$json_data["default"]["key_value"]

I thought there is something like $json_data[0] which should be "default". Apparently, this is also an array right? I've already looked around in the forums already, but unfortunately no one had the same JSON structure as their problems got solved. Maybe it's just dumb to use it like this, but there must be a way to make it work right? Possibly it's just 3 lines of code aswell and i'm not getting there right now... I've tried so much already.

我认为有一些像$ json_data [0]这应该是“默认”。显然,这也是一个数组吧?我已经在论坛中环顾四周,但不幸的是,没有人拥有相同的JSON结构,因为他们的问题得到了解决。也许这样使用它只是愚蠢,但必须有一种方法让它正常工作?可能它只是3行代码,我现在还没有到达那里......我已经尝试了很多。

Maybe you guys can help me with that.

也许你们可以帮助我。

2 个解决方案

#1


1  

I think what you want is this:

我想你想要的是这个:

foreach($json_data as $key=>$value) {
    echo $key, "<br />\n";
}

If you want it a bit nicer you can use array_keys():

如果你想要它更好一点,你可以使用array_keys():

foreach(array_keys($json_data) as $key) {
    echo $key, "<br />\n";
}

#2


1  

You have to try this loop :

你必须尝试这个循环:

foreach ($array as $key => $value) {
        // you could print here the key or value
    }

#1


1  

I think what you want is this:

我想你想要的是这个:

foreach($json_data as $key=>$value) {
    echo $key, "<br />\n";
}

If you want it a bit nicer you can use array_keys():

如果你想要它更好一点,你可以使用array_keys():

foreach(array_keys($json_data) as $key) {
    echo $key, "<br />\n";
}

#2


1  

You have to try this loop :

你必须尝试这个循环:

foreach ($array as $key => $value) {
        // you could print here the key or value
    }