3

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

1
  • Thanks guys! It works now, but strangely only after I removed the quick fix code I posted above.. You can test it yourself if you want. It's used in a Telegram Bot which informs about the meals of german university cafeterias. Send the "mensaID" (see openmensa.org) and get the meals of today. If you just want to try it you could use one of the following numbers: [ 1 , 210 , 138 ] Notice: most of them are closed on weekend Link to the BOT <- click here Commented Oct 17, 2015 at 10:07

4 Answers 4

4

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);
Sign up to request clarification or add additional context in comments.

Comments

1
<?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

Comments

1

Using a foreach loop you can add matching rows to a $query array that will hold any matching rows:

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

Outputs an array with these values:

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

Comments

0

Have you tried json_decode? Convert to a php array then use your foreach loop. http://php.net/manual/en/function.json-decode.php

Comments

Your Answer

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