Design Bytes Design Bytes - 1 year ago 109
PHP Question

Recursive function does not return array

I have written the function below to return parent id's of all categories in Worpress by category id. Everything works fine except it does not return the array... Any suggestions would be great! :-)

$current_category = (int) $_GET['current_category'];
$cat_ids = array($current_category);

function getParentCatIds($current_category,$cat_ids){
$child = get_category($current_category);
$parent_id = $child->parent;
if($parent_id !== 0){
array_push($cat_ids, $parent_id);
var_dump($cat_ids); // <--- this returns the right array
return $cat_ids; // <--- this returns NULL

$cat_ids = getParentCatIds($current_category,$cat_ids);
var_dump($cat_ids); // <--- this returns NULL

Answer Source

When you call getParentCatIds() (line 9), you didn't do anything with the return of the function. You should assign it or return it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download