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.

我正在使用CURL来检索项目的json列表。我正在尝试搜索每个项目并仅回显具有搜索词的项目。如果任何项目json字段匹配搜索词我想显示该项目。

        $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

json看起来像这样

(
[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'];
            }

        }   

1 个解决方案

#1


1

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

您正在将JSON解码为数组,这很好,但是从那时起您需要使用数组函数。例:

    $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;
   });  

更多相关文章

  1. 在php项目中, mysql视图常用吗?
  2. PHP数组值进入第二个数组[重复]
  3. 如何在php数组中插入新的键值对?
  4. mysql_fetch_array返回一个数组,数字为“1”
  5. PHP-从多维数组中删除重复值
  6. 更改数组键而不更改顺序
  7. 在PHP中更改关联数组索引的位置
  8. 如何在数组中存储产品数量
  9. php吧字符串直接转换成数组处理

随机推荐

  1. android studio 版本修改无效解决方案
  2. Android天气提醒程序源码
  3. [Android]Android Studio设置debug的keys
  4. Android(安卓)4.0 Service Framework
  5. Android设置竖屏
  6. android Style应用
  7. Android在listview添加checkbox实现
  8. 登录XML
  9. Drawable
  10. 2010.10.26———Android 01