How to use PHP to query the contents of a JSON array stored in a flat file and return the result?

Experts,

I have a flat file (users.php) with a JSON array stored within.

The JSON array takes the following format:
[{"name":"Sam Smith","profltitle":", MBA","location":"San Francisco","displayspecialty":"Accounting","headshotphoto":"2235396_photoweb.jpg","homepageurl":"http:\/\/example.com"},{"name":"Paula Jones","profltitle":", MA","location":"San Jose","displayspecialty":"Human Resources","headshotphoto":"2235555_photoweb.jpg","homepageurl":"http:\/\/example.com"}]

Open in new window


Users are allowed to specify a search term via a textbox:
<input name="searchfor" id="searchfor" type="text" />

Open in new window


Using the search term provided by the user I need to query the JSON array and pull results that match the given search term. The matching values need to be returned in a JSON format.

This is the programming logic as I see it now:

<?php
//DEFINE SEARCH TERM AS PROVIDED BY USER
$term = addslashes($_POST['searchfor']);
//DEFINE JSON ARRAY
$json_array = fopen('users.php');
//USING SEARCH TERM QUERY ARRAY
...
//PROVIDE THE RESULTS IN JSON FORMAT
...
?>

Any assistance would be helpful.
evibesmusicAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
First, don't use addslashes if you're going to search against the decoded array.

// Our search results
$results = array();

// JSON-decode the contents of users.php
$users = json_decode(file_get_contents("users.php"),true);

// Loop through users
foreach($users as $user)
{
  // Does the name field contain the search term?
  if(stripos($user["name"], $term) !== false)
  {
    // Yes, add user to $results
    $results[] = $user;
  }
}

// Display the JSON-encoded results
echo json_encode($results);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evibesmusicAuthor Commented:
@gr8gonzo:

Thank you very much. Works perfectly!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.