我有这行代码:
$roomservicesids = array_map(function($v){ return $v['serviceID'];}, $roomsservices);
它在我有PHP>的服务器上运行良好。 5.3在另一台服务器上,我有PHP< 5.3,它不起作用。我试图像这样重写它:
$roomservicesids = create_function('$v', 'return $v["serviceID"];,$roomsservices');
foreach ($services as $key1=>$value){
if(in_array($value['serviceID'], $roomservicesids)){ //error is in this line
echo "<input type='checkbox' name='services[]' id= '".$value['serviceID']."' value='" .$value['serviceID'] ."' checked = 'checked' class='zcheckbox' />";
}else{
echo "<input type='checkbox' name='services[]' id= '".$value['serviceID']."' value='" .$value['serviceID'] ."' class='zcheckbox' />";
}
echo "<label>" .$value['serviceName']. "</label>";
}
但我得到一个错误说:
Message: in_array() [function.in-array]: Wrong datatype for second argument
Line Number: 104
任何帮助都将深受赞赏。
答案 0 :(得分:3)
$roomservicesids = array_map(function($v){ return $v['serviceID'];}, $roomsservices);
这是获取每个数组元素的serviceID
的简洁方法。您可以使用如下的显式循环来执行此操作:
$roomservicesids = array();
foreach ($roomsservices as $service) {
$roomservicesids[] = $service['serviceID'];
}