-1

我有这行代码:

 $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

任何帮助将不胜感激。

4

1 回答 1

3
$roomservicesids = array_map(function($v){ return $v['serviceID'];}, $roomsservices);

这是获取serviceID每个数组元素的紧凑方法。您可以使用这样的显式循环来做到这一点:

$roomservicesids = array();

foreach ($roomsservices as $service) {
    $roomservicesids[] = $service['serviceID'];
}
于 2014-05-05T11:50:57.113 回答