How to Search Through a JSON Array in PHP

How to search through a JSON Array in PHP

Use the json_decode function to convert the JSON string to an array of object, then iterate through the array until the desired object is found:

$str = '{
"people":[
{
"id": "8080",
"content": "foo"
},
{
"id": "8097",
"content": "bar"
}
]
}';

$json = json_decode($str);
foreach ($json->people as $item) {
if ($item->id == "8097") {
echo $item->content;
}
}

php: loop through json array

Set the second function parameter to true if you require an associative array

Some versions of php require a 2nd paramter of true if you require an associative array

$json  = '[{"var1":"9","var2":"16","var3":"16"},{"var1":"8","var2":"15","var3":"15"}]';
$array = json_decode( $json, true );

Search item on json object PHP

Because you've passed the second parameter as true to json_decode, your $results variable is an array, so you need to access it like that:

foreach($results['sales'] as $item) {
foreach ($item['products'] as $product) {
if ($product['id'] == '1234') echo $product['name'];
}
}

Output:

Product 1

Demo on 3v4l.org

How to search through nested JSON arrays in PHP

In this case, simply do two loops.

$json = json_decode('{
"people": [
{
"id": "8080",
"content": "foo"
},
{
"id": "8097",
"content": "bar"
}
],
"dogs": [
{
"id": "8081",
"content": "spot"
},
{
"id": "8091",
"content": "max"
}
]
}');

foreach($json as $key=>$value){
//$key = people or dogs
//$value = stdClass()

foreach($value as $item)
{
if($item->id == "8097")
{
echo $item->content;
}
}
}

Output

bar

Sandbox

If we used 8081 instead of 8097 I would expect to get 'spot`. And testing that, I do indeed get that (which is in dogs).

How To Search In Multiple Columns In Json File Array?

Currently you search first in the 'id' column, then in the 'name' column, and finally in the 'symbol' column. If, at any stage, you encounter a match you return the array key. If you want to keep this functionality you have to look through the columns in that order.

You could combine the three columns into one array, juggle a bit with the keys, and do a single search that way, but I don't think that's very efficient.

Why not restructure you code a bit? For instance like this:

$query   = 'apple';
$columns = ['id', 'name', 'symbol'];
$data = json_decode(file_get_contents('file.json', true), true);
foreach ($columns as $column) {
$key = array_search($query, array_column($data, $column);
if ($key !== false) break;
}
var_dump($key);

Now you've only used array_search() once, in a way.

This code is more efficient than yours because it stops searching as soon as it has found 'apple' in a column. Your code always searches through all columns.

Note that I actually check that array_search() returns false, unlike what you did, which would not have responded when this functions returned key zero.

Also note that, if ever the need arises, you can now easily add more columns without having to add more repeating lines of code.

Search through JSON array for matching key

It should do the trick:

foreach ($json['Response']['data']['entries'][0]['extended']['values'] as $key => $value) {
if (false !== strpos($key, 'medals')) {
echo $value.PHP_EOL;
}
}

Alternatively, you can use array_filter:

var_dump(array_filter($json['Response']['data']['entries'][0]['extended']['values'], function ($k, $v) {
return false !== strpos($k, 'medals');
}, ARRAY_FILTER_USE_BOTH));

How to search variable through a JSON Array in PHP

foreach($response_array->id as $item)

should be

foreach($response_array as $item)

Search through a json array obtained from a database

Probably the quickest way would be strpos function, so you can use it this way

function hasEmail($string, $email)
{
return strpos($string, $email) !== false;
}

//example
echo hasEmail($resultEmail, $email) ? 'Has email' : 'Email not found';


Related Topics



Leave a reply



Submit