I am trying to create a search a function in php which searches for the data contained inside a Json file. I am able to do this for a normal text file which returns a list of results based on what has been entered in the search field which returns closet match. However does not work when doing this same for a json file and does not return anything.
search.php
if (isset($_POST) && isset($_POST['txt'])) {
$search = $_POST['txt'];
$file = file('data/contacts.json');
$found = false;
$jsonData = json_decode($file, true);
foreach ($jsonData as $line) {
if (strpos($line, $search) !== false) {
$found = true;
echo $line;
}
}
if (!$found) {
echo 'No match found';
}
}
contacts.json
[
{
"id": 3,
"forename": "John",
"surname": "Smith",
"email": "j@hotmail.com",
"address": "addresss",
"telephone": "01232302323"
},
{
"id": 4,
"forename": "Keith",
"surname": "Miller",
"email": "K@hotmail.com",
"address": "ssdsds",
"telephone": "01232302323"
},
{
"id": 5,
"forename": "Doug",
"surname": "Howard",
"email": "d@hotmail.com",
"address": "test",
"telephone": "01232302323"
},
{
"id": 2,
"forename": "r",
"surname": "r",
"email": "email@gmail.com",
"address": "test",
"telephone": "01232302323"
}
]