-1

I have this line of code :

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

It works great on a server where I have PHP > 5.3 On another server, where I have PHP < 5.3, it doesn't work. I am trying to rewrite it like this:

 $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>";

  }  

But I get an error that say:

Message: in_array() [function.in-array]: Wrong datatype for second argument
Line Number: 104

Any help will be deeply appreciated.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user2417624
  • 653
  • 10
  • 32
  • `$roomservicesids` is a function. Why are you passing it to `in_array`? – Oliver Charlesworth May 05 '14 at 11:49
  • I am trying to check if the serviceID exists in both arrays and to print the form only once. you can see more here: http://stackoverflow.com/questions/22460469/looping-trough-2-array-to-find-out-which-checkboxes-should-be-checked/22460792#22460792 – user2417624 May 05 '14 at 11:56

1 Answers1

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

This is a compact way of getting the serviceID of each array element. You can do it with an explicit loop like so:

$roomservicesids = array();

foreach ($roomsservices as $service) {
    $roomservicesids[] = $service['serviceID'];
}
Manoj Yadav
  • 6,560
  • 1
  • 23
  • 21
John Kugelman
  • 349,597
  • 67
  • 533
  • 578