简体   繁体   English

JSON:获得具有特定价值的条目

[英]JSON: Getting entry with specific value

I have the following JSON array, in this case it has only three entries, but there could also be more. 我有以下JSON数组,在这种情况下它只有三个条目,但也可能有更多。

[
    {
        "id": 45,
        "text": "apple"
    },
    {
        "id": 37,
        "text": "pear"
    },
    {
        "id": 22,
        "text": "strawberry"
    }
]

Now my question is: How do I get the text variable of the entry with (for example) id being 37 in PHP? 现在我的问题是:如何在PHP中获取条目的text变量(例如) id37 Is it possible to get that easily? 有可能轻松搞定吗?

What I know: I could use a for loop for finding the text like this (in PHP): 我所知道的:我可以使用for循环来查找这样的文本(在PHP中):

<?php
    $fruits = json_decode(file_get_contents("file.json"), true); // First I decode the file
    for ($i = 0; $i < count($fruits); $i++) {
        // Using this for loop and if condition, I get the text I need
        if ($fruits[$i]['id'] == 37) echo $fruits[$i]['text'];
    }
?>

But I don't want to use a for loop, as my JSON array has more than 3 entries, and if i request a lot of data in short time it takes long time till the for loop goes through every entry. 但是我不想使用for循环,因为我的JSON数组有超过3个条目,如果我在短时间内请求大量数据,则for循环遍历每个条目需要很长时间。 So is there a more effective way to get to the same result? 那么有更有效的方法来获得相同的结果吗? Can somebody explain me this in PHP? 有人可以用PHP解释我吗?

The solution with array_filter() : array_filter()的解决方案:

$yourEntry = current(array_filter($jsonArray, function(\stdClass $entry) {
    return ($entry->id == 37);
}));

See the example 查看示例

Change this code: 更改此代码:

<?php
    $fruits = json_decode(file_get_contents("file.json"), true); // First I decode the file
    for ($i = 0; $i < count($fruits); $i++) {
        // Using this for loop and if condition, I get the text I need
        if ($fruits[$i]['id'] == 37) echo $fruits[$i]['text'];
    }
?>

to

<?php
    $fruits = json_decode(file_get_contents("file.json"), true); // First I decode the file
    foreach ($fruits as $fruit) {
        // Using this for loop and if condition, I get the text I need
        if ($fruits['id'] == 37)  { 
        echo $fruits['text'];
        //rest of your code you like to add
        }
    }
?>

if you intend to use for loop only then use below code: 如果你打算只使用for循环,那么使用下面的代码:

<?php
    $fruits = json_decode(file_get_contents("file.json"), true); // First I decode the file
    for ($i = 0; $i < count($fruits); $i++) {
        // Using this for loop and if condition, I get the text I need
        if ($fruits['id'] == 37)  { 
        echo $fruits['text'];
        //rest of your code you like to add
        }
    }
?>

You could do it simply with this: 你可以用这个简单地做到这一点:

foreach($fruits as $item) {
    if($item['id'] == 37) { echo $item['text']; break; }
}

Example

if you can change your json structure then it is possible. 如果你可以改变你的json结构,那么它是可能的。

If you create json like this 如果你像这样创建json

{
    "45":{ 
        "text": "apple"
    },
    "37":{ 
        "text": "pear"
    },
    "22":{
        "text": "strawberry"
    }
}

and in php 在PHP中

 echo $item['37']['text'];

This will help :) 这将有助于:)

Are you in control of the json data structure? 你是否控制了json数据结构? If so, you could change to access via array key, eg 如果是这样,您可以通过数组键更改为访问,例如

{
  '37': 'pear',
  '45': 'apple',
  '22': 'strawberry'
}

$fruits = json_decode(file_get_contents("file.json"), true); $ fruits = json_decode(file_get_contents(“file.json”),true);

echo $fruits['37']; echo $ fruits ['37']; // pear // 梨

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM