php - Function in function not returning array -
this function should iterate through multidimensional array , return collection of array's element_rule_id
values. hovewer it's not returning anything.
you can check var_dump of array here: http://pastebin.com/t5nwgmna
function deepins($array = array(), $collect = array(), $str = '') { $count = count($array); foreach($array $i => $val) { if(is_array($val)) { if(array_key_exists('element_rule_id' ,$val)) { $collect[$val['element_rule_id']] = 1; } if(($count - 1) == $i) { if(array_key_exists('0', $val['condition'])) { deepins($val['condition'], $collect); } else { return $collect; } } } } }
expected result be:
array (size=5) 'rule_demo_rules1_1' => int 1 'rule_demo_rules1_2' => int 1 'rule_demo_rules1_3' => int 1 'rule_demo_rules1_5' => int 1 'rule_demo_rules1_6' => int 1
in desining of recursive functions, returning ambigous way, simple solution put function in static class, , add return results class:
class xx { public static $result; function deepins($array = array(), $collect = array(), $str = '') { $count = count($array); foreach($array $i => $val) { if(is_array($val)) { if(array_key_exists('element_rule_id' ,$val)) { $collect[$val['element_rule_id']] = 1; } if(($count - 1) == $i) { if(array_key_exists('0', $val['condition'])) { deepins($val['condition'], $collect); } else { self::$result[] = $collect; } } } } }
Comments
Post a Comment