-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.

2
  • $roomservicesids is a function. Why are you passing it to in_array? Commented May 5, 2014 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: stackoverflow.com/questions/22460469/… Commented May 5, 2014 at 11:56

1 Answer 1

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'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Manoj. I overlook that. I will accept your answer as soon as system allows me to do so...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.