0

I am trying to search through JSON with PHP with a value given in an HTML form. I have the user enter the name of who they would like to search and I want it to look through the JSON for the their name.

JSON Example:

    {"responseData": {"total": 1775, "results": [{"wiki": "A._C._Grayling", "url": "/thinker/3118", "sep_dir": "", "label": "A. C. Grayling", "type": "thinker", "ID": 3118}

PHP code:

    $submit_value = $_POST['regular_philo'];
function searchinPho_json($type, $id, $obj, $label, $submit_value){

    global $philo;
    $base = "https://inpho.cogs.indiana.edu";
    $format = "json";
    $url = $base. '/' . $type . '/' . $id . '.' . $format;
    $data = @file_get_contents($url, o, null, null);

    foreach($obj as $item) {
    foreach($item as $philo) {
        if(isset($philo->$label) && $philo->$label == $submit_value) {
            return $philo;
        }
    }
}
return null;

}




$data = searchinPho_json('thinker', 'id');
$json = json_decode($data);
$url = $json->url;
$label = $json->label;

How can I get it to search through with the name given and output the ID needed for the specific search?

3
  • Possible duplicate of How to search through a JSON Array in PHP Commented Nov 30, 2015 at 22:18
  • That does help when looking for it. But my problem comes from using a json data from a website. Since I have input such as "Plato" I have to find the name then return its ID to put in the searchinPho_json $id area. Commented Nov 30, 2015 at 22:59
  • It doesn't matter where the JSON comes from, you should be able to search it in the same way. Commented Dec 1, 2015 at 12:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.