1

I'm using CURL to retrieve a json list of projects. I'm trying to search each project and echo only the projects that have the search term. If any of the projects json fields match The search term I would like to display that project.

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://url.com/api/projects");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          "Access-Token: CsdujazxcvSq0w"
        ));
        $proj_srch = curl_exec($ch);
        curl_close($ch);
        $json = json_decode($proj_srch, TRUE);

The json looks like this

(
[success] => 1
[data] => Array
    (
        [0] => Array
            (
                [id] => 1088
                [project_name] => Mission Church
                [project_no] => 1088
                [description] => ries, but also the leap into electronic typesetting, remaining essentially unchanged.
                [institution_name] => Archdio
                [address] => 35 Ptt Street
                [city] => Suaa
                [state_province] => Suaa
                [country] => Fiji
                [country_code] => fj
                [postal_code] => 00000
                [dio_name] => Dio of Suaa
                [featured_image_url] => https://url.org/media/cd51ec97-2684-6ebb-48c8-89d00b9685d3.JPG
            )

        [1] => Array
            (
                [id] => 1100
                [project_name] => Micro-financing
                [project_no] => 1100
                [description] => ries, but also the leap into electronic typesetting, remaining essentially unchanged. 
                [institution_name] => Missions of Africa
                [address] => PO Box 9253
                [city] => Macha
                [state_province] => Macha
                [country] => Kenya
                [country_code] => ke
                [postal_code] => 87199
                [dio_name] => Dio of Macha
                [project_leader] => 55
                [project_leader_name] =>  Bowman
                [status] => published
                [featured_image_url] => https://url.com/media/31bdace8-a384-832c-3976-666f4d2f7bfd.JPG

            )

This is what I tried with no success

       if (strpos($json, 'search-term') !== false) {

            foreach ($json['data'] as $key ) {
            echo    $key['project_name'];
            }

        }   
7
  • 1
    What you claim is JSON is not really JSON Commented Jul 11, 2016 at 15:28
  • 1
    An array. It says so in the dump. Commented Jul 11, 2016 at 15:31
  • This makes no sense: strpos($json $json used as STRING then $json['data'] useing it as ARRAY !? When do you use json_decode(); ? Commented Jul 11, 2016 at 15:33
  • I used json_decoade() in the first block of code shown Commented Jul 11, 2016 at 15:34
  • 1
    Then strpos($json, 'search-term') !== false wont work! Commented Jul 11, 2016 at 15:35

1 Answer 1

1

You are decoding the JSON into an array, which is good, however you need to use array functions from then on. Example:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://url.com/api/projects");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      "Access-Token: CsdujazxcvSq0w"
    ));
    $proj_srch = curl_exec($ch);
    curl_close($ch);
    $json = json_decode($proj_srch, TRUE);

    $searchTerm = "search-term"; //Or whatever
    $json["data"] = array_filter($json["data"], function ($v) use ($searchTerm) { 
          foreach ($v as $entry) { 
                if (is_string($entry) && strpos($entry,$searchTerm) !== false) { return true; }
          }
          return false;
   });  
Sign up to request clarification or add additional context in comments.

6 Comments

Hey, really strpos() !== 0?
What if the search-term begins at pos 0? ;)
@JustOnUnderMillions my mistake. Long day
The only valid version is strpos($entry,$searchTerm) !== false :)
Seem to be working but how do I display the full project name $entry['project_name']; only seems to be displaying the first letter of the project name
|

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.