简体   繁体   中英

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.

[
    {
        "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? Is it possible to get that easily?

What I know: I could use a for loop for finding the text like this (in 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. So is there a more effective way to get to the same result? Can somebody explain me this in PHP?

The solution with 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:

<?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.

If you create json like this

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

and in php

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

This will help :)

Are you in control of the json data structure? 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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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