检查数组中的密钥是否等于值
问题描述:
嘿,我有小问题,我寻找快速的PHP解决方案,我的问题。 所以我就阵列是这样的:检查数组中的密钥是否等于值
services: [
{
service_id: 8,
service_name: "Air Condition - Cleaning",
service_time_realisation: "30",
service_price: "300"
},
{
service_id: 6,
service_name: "Carpet Cleaning",
service_time_realisation: "10",
service_price: "100"
}
],
我如何检查此数组中的每个对象的service_id是例如“7”?
请问你能帮我吗? ;)
答
尝试另一个方法:
function checkJson($json, $id)
{
foreach(json_decode($json, true) as $service) {
if($service['service_id']!=$id) {
return false;
}
}
return true;
}
$str = '[
{
service_id: 8,
service_name: "Air Condition - Cleaning",
service_time_realisation: "30",
service_price: "300"
},
{
service_id: 6,
service_name: "Carpet Cleaning",
service_time_realisation: "10",
service_price: "100"
}
]';
$result = checkJson($str, 7);
答
$retour = false;
foreach($services as $row){
if($row['service_id'] == 7){
$retour = true;
}
}
如果RETOUR ==真正的数据包含7
嗯......遍历每个对象和检查? –
到目前为止您尝试了什么? –
好吧,它已经好了;)我弄清楚一些好东西:P –