我收到了每個運動員的學校名單。如果運動員在對象中的狀態爲committed: true
,我只想顯示該學校。否則,我想返回所有屬性。如何檢查在PHP中是否存在鍵/值對?
這是我的數據是這樣的:
...
"offers": [
{
"school": "Foo School",
"committed": true
},
{
"school": "Bar School",
"committed": false
}
]
...
在上面的例子中,我只是想顯示「富校」。 但如果committed
屬性都是false
我想顯示「Foo Schoo」和「酒吧學校」。
這是我目前的,但我不管回來。
foreach ($object['athlete']['offers'] as $offer) {
if (isset($offer['committed']) && $offer['committed'] == 1) {
// return single school
} else {
// push into array & return?
}
}
謝謝你的任何建議!
那麼問題是什麼? – Alexander
如果offer陣列中的任何商品具有「committed:true」屬性,則只返回該商品。否則,返回數組中的所有商品。 – Damon
它可能是混合數組/對象造成衝突嗎?你可能需要'$ offer-> committed'的語法。你在這裏使用'json_decode()'嗎? – Scuzzy