【问题标题】:PHP search in json arrayPHP在json数组中搜索
【发布时间】:2016-01-15 15:40:22
【问题描述】:

我想获取所有 "name" 键的值,其中 "category" 值不是“Beilagen”或“Aktion”。

使用以下 json 数据的所需输出示例:

Zartweizen mit Gemüse

Kaiserschmarrn mit Apfelmus

Gebackene Tintenfischringe mit Knoblauchdip

解决方案可能是一个 foreach 循环,但我真的不知道如何使用它进行特定搜索。

这是我的快速修复解决方案,但正如您在下面的 json 示例中所见,相关数据的数量各不相同,而且我必须获得的并不总是 4 个名称。可以多也可以少。

$name = "1. ".$updateArrayMensa[0]["name"].chr(10)."2. ".$updateArrayMensa[1]["name"].chr(10)."3. ".$updateArrayMensa[2]["name"].chr(10)."4. ".$updateArrayMensa[3]["name"];

Json 数据示例:

[
   {
      "id":1542115,
      "name":"Zartweizen mit Gemüse",
      "category":"Tagesgericht 1",
      "prices":{
         "students":1.0,
         "employees":1.9,
         "pupils":null,
         "others":2.4
      },
      "notes":[
         "veganes Gericht"
      ]
   },
   {
      "id":1542116,
      "name":"Kaiserschmarrn mit Apfelmus",
      "category":"Tagesgericht 4",
      "prices":{
         "students":2.4,
         "employees":2.95,
         "pupils":null,
         "others":3.45
      },
      "notes":[
         "mit Antioxidationsmittel",
         "fleischloses Gericht"
      ]
   },
   {
      "id":1542117,
      "name":"Gebackene Tintenfischringe mit Knoblauchdip",
      "category":"Aktionsessen 3",
      "prices":{
         "students":2.4,
         "employees":2.95,
         "pupils":null,
         "others":3.45
      },
      "notes":[
         "mit einer Zuckerart und Süßungsmitteln",
         "mit Farbstoff",
         "mit Fleisch"
      ]
   },
   {
      "id":1542128,
      "name":"Ananaskompott",
      "category":"Beilagen",
      "prices":{
         "students":null,
         "employees":null,
         "pupils":null,
         "others":null
      },
      "notes":[
         "veganes Gericht"
      ]
   },
   {
      "id":1542129,
      "name":"Weiße Schokolade-Himbeer-Cookie",
      "category":"Aktion",
      "prices":{
         "students":null,
         "employees":null,
         "pupils":null,
         "others":null
      },
      "notes":[
         "fleischloses Gericht"
      ]
   }
]

这个json数据的来源:http://www.openmensa.org/api/v2/canteens/138/days/2015-10-16/meals

【问题讨论】:

  • 谢谢大家!它现在可以工作,但奇怪的是,只有在我删除了上面发布的快速修复代码之后。如果你愿意,你可以自己测试它。它用于 Telegram Bot,用于通知德国大学自助餐厅的用餐情况。发送“mensaID”(请参阅​​ openmensa.org)并获得今天的饭菜。如果您只是想尝试一下,您可以使用以下数字之一:[ 1 , 210 , 138 ] 注意:大多数在周末休息 Link to the BOT <- click here

标签: php arrays json api foreach


【解决方案1】:

您可以使用函数 json_decode 将 json 数组转换为简单的 php 数组:

$items = json_decode("you_json_string");

$names = array();

foreach($items as $item) {
   if($item->category == 'Beilagen' || $item->category == 'Action')
       continue;

   $names[] = $item->name;
}
print_r($names);

【讨论】:

    【解决方案2】:
    <?php
    
      $json = json_decode( file_get_contents( 'http://www.openmensa.org/api/v2/canteens/138/days/2015-10-16/meals' ) );
      $drop = array( 'Beilagen', 'Aktion' );
      $name = array();
    
      foreach( $json as $obj ) {
        if ( !in_array( $obj->category, $drop ) ) {
          $name[] = $obj->name;
        }
      }
    
      echo implode( '<br>', $name );
    
    ?>
    

    输出:

    Zartweizen mit Gemüse
    Kaiserschmarrn mit Apfelmus
    Gebackene Tintenfischringe mit Knoblauchdip
    

    【讨论】:

      【解决方案3】:

      使用 foreach 循环,您可以将匹配的行添加到将包含任何匹配行的 $query 数组:

      $json = file_get_contents("9880387.json");
      $json = json_decode($json);
      
      # print_r($json);
      
      foreach ($json as $v) {
          if( !in_array($v->category,["Beilagen","Aktion"]) ) {
              $query[] = $v->name;
          }
      }
      print_r($query);
      

      输出具有这些值的数组:

      Zartweizen mit Gemüse
      Kaiserschmarrn mit Apfelmus
      Gebackene Tintenfischringe mit Knoblauchdip
      

      【讨论】:

        【解决方案4】:

        你试过json_decode吗?转换为 php 数组,然后使用你的 foreach 循环。 http://php.net/manual/en/function.json-decode.php

        【讨论】:

          猜你喜欢
          • 2011-10-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-22
          • 2015-07-06
          • 1970-01-01
          相关资源
          最近更新 更多