Link to home
Start Free TrialLog in
Avatar of evibesmusic
evibesmusicFlag for United States of America

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of evibesmusic

ASKER

@gr8gonzo:

Thank you very much. Works perfectly!