Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP search in json array

I want to get the value of all "name" keys which "category" value is not "Beilagen" or "Aktion".

Example of the wanted ouput using the json data below:

Zartweizen mit Gemüse

Kaiserschmarrn mit Apfelmus

Gebackene Tintenfischringe mit Knoblauchdip

The solution may be a foreach loop, but I can't really figure out how to make specific searches with that.

This is my quick fix solution, but as you can see in the json example below, the number of relevant data varies and it's not always 4 names I have to get. It can be more or less than that.

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

Json data example:

[
   {
      "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"
      ]
   }
]

Source of this json data: http://www.openmensa.org/api/v2/canteens/138/days/2015-10-16/meals


2 Answers

You can convert json array to simple php array with function json_decode:

$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);
like image 92
Roman Avatar answered Sep 03 '26 21:09

Roman


<?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 );

?>

Output:

Zartweizen mit Gemüse
Kaiserschmarrn mit Apfelmus
Gebackene Tintenfischringe mit Knoblauchdip
like image 34
Tom Avatar answered Sep 03 '26 20:09

Tom