Answer the question
In order to leave comments, you need to log in
How to get recursion correctly?
I have a multidimensional array and I want to receive data regardless of nesting level.
example I need to get data by id.
public function getChildById($arr, $id) {
if(is_array($arr)){
foreach ($arr as $key => $value) {
if($value['_id'] == $id){
return $value;
}else {
if($this -> getChildById($value['childs'], $id) != null){
print_r(1);
return $arr[$key]['childs'][0];
}
}
}
}
return false;
}
Answer the question
In order to leave comments, you need to log in
if($this->getChildById($value['childs'], $id) != null){
print_r(1);
return $arr[$key]['childs'][0];
}
if ($r = $this->getChildById($value['childs'], $id) !== false) {
return $r;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question