Robinson Posted April 15, 2020 Share Posted April 15, 2020 Dear all, Kindly help with this code I have written, it is not returning the array I created. It is a very small script but I have been looking for a very long time but I couldn't see why it went wrong. $duration = 2; $start='01-01-2019'; $month =date('n', strtotime($start)); $year =date('Y', strtotime($start)); $mon = 12-$month+1; $remainingMonth = $duration*12; $years_arr = array("$year"); $residue_arr = array("$remainingMonth"); $years_arr = process($remainingMonth,$year,$mon,$years_arr,$residue_arr); print_r($years_arr); //<output function process($remainingMonth,$year,$mon,$years_arr,$residue_arr){ $residue = $remainingMonth - $mon;//$D$18-D20 if($residue != 0){ $new_year = $year+1; array_push($years_arr, $new_year); array_push($residue_arr, $residue); if($residue > 12 ){ $mon = 12; } else { $mon = $residue; } echo "<br>"; process($residue,$new_year,$mon,$years_arr,$residue_arr); } else { print_r($years_arr); return $years_arr; } }//end of process(..) The objective is to add a year to the $years_arr array and then print it put, yet I notice that nothing get's printed out. Please assist, if you need more info from my side, please do ask. Thank you very much in advance. Quote Link to comment Share on other sites More sharing options...
chhorn Posted April 15, 2020 Share Posted April 15, 2020 (edited) replace print_r with var_dump to see what the variables actually contain. Check every condition if it really matches your expected values, like var_dump($residue) and so on. As this is a recursive function it may be of interest to have a static iteration number and all given function arguments to display. As this is a working example (good!) you can test this first online to see if may be a configuration issue on your machine https://3v4l.org/VC646 Edited April 15, 2020 by chhorn Quote Link to comment Share on other sites More sharing options...
TrueMember Posted April 15, 2020 Share Posted April 15, 2020 (edited) You can remove " of this two lines: $years_arr = array($year); $residue_arr = array($remainingMonth); Edited April 15, 2020 by TrueMember Quote Link to comment Share on other sites More sharing options...
Barand Posted April 15, 2020 Share Posted April 15, 2020 (edited) The design of your recursive function isn't quite right. $duration = 2; $start='01-01-2019'; $month =date('n', strtotime($start)); $year =date('Y', strtotime($start)); $mon = 12-$month+1; $remainingMonth = $duration*12; $years_arr = array("$year"); $residue_arr = array("$remainingMonth"); $years_arr = process($remainingMonth,$year,$mon,$years_arr,$residue_arr); // THIS CALL RETURNS NOTHING print_r($years_arr); //<output function process($remainingMonth,$year,$mon,$years_arr,$residue_arr){ $residue = $remainingMonth - $mon;//$D$18-D20 if($residue != 0){ $new_year = $year+1; array_push($years_arr, $new_year); array_push($residue_arr, $residue); if($residue > 12 ){ $mon = 12; } else { $mon = $residue; } echo "<br>"; process($residue,$new_year,$mon,$years_arr,$residue_arr); // RETURNS VALUE BUT IS IGNORED } else { print_r($years_arr); return $years_arr; } }//end of process(..) The first call (lline 11) returns nothing as the if/else calls the process() function instead. This second call does go through the else{} branch and return the array. However your call does not collect the value returned and so, when the function exits, nothing is returned. You need to capture the result from that internal call to process() and return that value as the result so it is passed up the chain of calls, thus... function process($remainingMonth,$year,$mon,$years_arr,$residue_arr){ $residue = $remainingMonth - $mon;//$D$18-D20 if($residue != 0){ $new_year = $year+1; array_push($years_arr, $new_year); array_push($residue_arr, $residue); if($residue > 12 ){ $mon = 12; } else { $mon = $residue; } echo "<br>"; $years_arr = process($residue,$new_year,$mon,$years_arr,$residue_arr); // GET RETURNED VALUE } else { // print_r($years_arr); return $years_arr; } return $years_arr; // PASS IT BACK }//end of process(..) [edit] P.S. Is this a competition entry for the most convoluted code to return an array of 2 consecutive year numbers? Edited April 15, 2020 by Barand Quote Link to comment Share on other sites More sharing options...
Robinson Posted April 16, 2020 Author Share Posted April 16, 2020 Thank you Barand, I have tried out your suggestion and it has worked. However, I have one question, it is about these lines: } else { print_r($years_arr); return $years_arr; How is it that it manages to print $years_arr but it doesn't return the $years_arr. Also, can you point me to some resources(books, websites) that teach about Recursion in PHP, they are hard to come by and most books, at least the ones I obtained, delve on the subject very briefly. And, no, this is not an entry into a competition :)), it is a small portion of a bigger program. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 16, 2020 Share Posted April 16, 2020 4 hours ago, Robinson said: How is it that it manages to print $years_arr but it doesn't return the $years_arr. It did return the array - you were ignoring the returned value (the return value was not assigned to a variable) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.