Welcome Guest, Not a member yet? Register   Sign In
php function will echo but returned value is empty
#1

[eluser]skunkbad[/eluser]
I'm working with a function that is just plain php. The function creates the string that I am looking for, so it does work, but it won't return the string. Code below shows echo $path, but if I return $path, the string should be output by var_dump(). Anyone see the problem why the return doesn't work?

Code:
<?php

$all_categories_array = array(
    '0' => array(
                'category_id' => '1',
                'category_name' => 'Examples',
                'parent_id' => '0'
    ),
    '1' => array(
                'category_id' => '2',
                'category_name' => 'Widgets',
                'parent_id' => '1'
    ),
    '2' => array(
                'category_id' => '3',
                'category_name' => 'Little',
                'parent_id' => '1'
    ),
    '3' => array(
                'category_id' => '4',
                'category_name' => 'Hardcore',
                'parent_id' => '2'
    ),
);


function get_product_parents($id, $all_categories_array, $steps)
{
    // check the entire categories array for a matching category
    foreach($all_categories_array as $x => $y)
    {
        // if this is the product's category
        if($y['category_id'] == $id)
        {
            // add the category name to the steps array
            $steps[] = $y['category_name'];

            // and do recursion if not a top level category
            if($y['parent_id'] !== '0')
            {
                get_product_parents($y['parent_id'], $all_categories_array, $steps );
                exit;
            }
            else
            {
                $reverse_array = array_reverse($steps);
                $path = '';
                foreach($reverse_array as $category_name)
                {
                    $path .= '/' . $category_name;
                }

                // Should be /Examples/Widgets/Hardcore
                echo $path;
            }
        }
    }

}

$steps = array();
$whatever = get_product_parents('4', $all_categories_array, $steps);
var_dump($whatever);


Messages In This Thread
php function will echo but returned value is empty - by El Forum - 06-07-2010, 02:22 AM
php function will echo but returned value is empty - by El Forum - 06-07-2010, 02:35 AM
php function will echo but returned value is empty - by El Forum - 06-07-2010, 02:43 AM
php function will echo but returned value is empty - by El Forum - 06-07-2010, 02:52 AM
php function will echo but returned value is empty - by El Forum - 06-07-2010, 03:03 AM
php function will echo but returned value is empty - by El Forum - 06-07-2010, 03:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB