What I'm trying to do:
treat the POST data from a multi-select input with the array_diff() function
Initial code:
$relations_to_delete=array_diff($selectedEnjeuxMetiers,$this->request->data['EnjeuxMembership']['EnjeuxMetier']);
Probem: It was not working when nothing was selected in the multiselect input
Current solution:
if(!empty($this->request->data['EnjeuxMembership']['EnjeuxMetier'])){
$relations_to_delete=array_diff($selectedEnjeuxMetiers,$this->request->data['EnjeuxMembership']['EnjeuxMetier']);
}else{
$relations_to_delete=$selectedEnjeuxMetiers;
}
This solution works. !=null was not working, nor gettype()=="array"
Question: Could anyone could explain why the if(!empty()) test is necessary, and if the problem comes from the POST data or the array_diff function?
EDIT: It works with gettype()=="array". The problem was that the type when there is no data is not an empty array but an empty string.
Additional info: CakePHP docs about the way Post data are converted to an array.