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 variable text entries with (for example) id like 37 in PHP? Is it easy to do this?
What I know: I could use a for loop to find 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, since my JSON array has more than 3 records, and if I request a lot of data in a short time, it will take a long time until the for loop goes through each record. So is there a better way to get the same result? Can someone explain this to me in PHP?
source share