how to search inside two json files at a time in php?

Since you are looping through json file it will search inside of “data” so you can’t use $v['data']

Also since edges is also array that have same values and you want to search inside of it you must make loop on that also

Here is example

foreach ($result2 as $k => $v) {
    foreach ($v['business']['customers']['edges'] as $val) {
        if ($val['node']['name'] == 'Trump') {
            echo '<pre>';
            // and now you can access any of those values, echo $val['node']['id'];
            print_r($val['node']);
            echo '</pre>';
        }
    }
}

Output

Array
(
    [id] => QnVzaW5l5Gy9
    [name] => Trump
    [email] => [email protected]
)

EDIT

Since you want to search in both files at a same time you can put them into array and then use it like this

$json1 = file_get_contents("1.json");
$json2 = file_get_contents("2.json");

$result1 = json_decode($json1, true);
$result2 = json_decode($json2, true);

$joined_result = array($result1, $result2);

foreach($joined_result as $val) {
    // this will take all nodes where you will search
    $node = $val['data']['business']['customers']['edges'];

    foreach ($node as $value) {
        if ($value['node']['name'] == 'Trump') {
            echo '<pre>';
            print_r($value['node']);
            echo '</pre>';
        }
    }
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top