Reputation:
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
Upvotes: 3
Views: 7287
Reputation: 4094
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
Upvotes: 1
Reputation: 691
<?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
Upvotes: 1
Reputation: 41
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);
Upvotes: 4
Reputation: 183
Have you tried json_decode? Convert to a php array then use your foreach loop. http://php.net/manual/en/function.json-decode.php
Upvotes: 0