sKunKbad Posted June 7, 2010 Share Posted June 7, 2010 Just getting tired, and don't see my problem. Output should be: /Examples/Widgets/Hardcore but all I am getting is: /Hardcore Code Example: <?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) { 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 ); } } } $reverse_array = array_reverse($steps); $path = ''; foreach($reverse_array as $category_name) { $path .= '/' . $category_name; } return $path; } $steps = array(); $link_path = get_product_parents('4', $all_categories_array, $steps); // Should be /Examples/Widgets/Hardcore echo $link_path; Link to comment https://forums.phpfreaks.com/topic/204073-recursive-function/ Share on other sites More sharing options...
trq Posted June 7, 2010 Share Posted June 7, 2010 You never actually capture the returned value from your recursive call for one. Link to comment https://forums.phpfreaks.com/topic/204073-recursive-function/#findComment-1068883 Share on other sites More sharing options...
sKunKbad Posted June 7, 2010 Author Share Posted June 7, 2010 This actually works, as I can echo $path, but if I try to return $path it is empty: <?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); Link to comment https://forums.phpfreaks.com/topic/204073-recursive-function/#findComment-1068886 Share on other sites More sharing options...
sKunKbad Posted June 7, 2010 Author Share Posted June 7, 2010 Thorpe, thanks. I should have read what you said and applied it to the code, because that was all it needed. Link to comment https://forums.phpfreaks.com/topic/204073-recursive-function/#findComment-1068895 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.