【发布时间】: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