1

I'm assuming my loop keeps looping and clearing my temp array, but not sure how to fix this. At the end, return is always empty.

How can I correctly return my temp array?

Data set:

Array(
    [0] => Array(
            [0] => Array(
                    [id] => 55
                    [parent] => 49
                )
            [1] => Array(
                    [id] => 62
                    [parent] => 50
                )
            [2] => Array(
                    [id] => 64
                    [parent] => 51
                )
        )
    [1] => Array(
            [0] => Array(
                    [id] => 49
                    [parent] => 0
                )
        )
)

Function:

<?php

    $patterns = function($array, $temp = array(), $index = 0, $parent = 0) use(&$patterns) {
        if($index < count($array)) {
            foreach($array[$index] as $sub) {
                if($index == 0 || $parent == $sub['id']) {
                    $temp[$index] = $sub['id'];
                    $patterns($array, $temp, $index + 1, $sub['parent']);
                }
            }
        }

        if($index >= count($array) && $parent == 0) {
            print_r($temp); // correct result does display here!

            return $temp; // this return gives no return
        }
    };

    print_r($patterns($dataset));

?>

print_r returns Array ( [0] => 55 [1] => 49 )

HelpNeeder
  • 6,383
  • 24
  • 91
  • 155

2 Answers2

1

On line 8, return the result of $patterns($array, $temp, ...) only if it is not null. Also, don't set the result to the $temp variable, so you don't override it if the result is null.

Like so:

$temp2 = $patterns($array, $temp, $index + 1, $sub['parent']);

if (isset($temp2)) {
    return $temp2;
}

If the condition on line 13 fails, it'll return null, and that's not the result you want, so you have to keep going if it is null.


By the way, I wasn't able to reproduce your code giving the correct answer where it says // correct result does display here!. To make it work I had to change lines 5 & 6 to:

foreach($array[$index] as $key => $sub) {
    if ($key == 0 || $parent == $sub['id']) {

And I also had to change line 13 to:

if($index >= count($array)-1 && $parent == 0) {
Matthew L
  • 26
  • 1
  • 4
  • thank you so much! Line 5, 6, and 13 changes broke the logic. The returning the variable worked! – HelpNeeder Nov 18 '15 at 04:19
  • Ah just realized that my datA set is already reversed so no need to reverse again. This is the reason you couldn't get the result. My mistake. – HelpNeeder Nov 19 '15 at 23:22
0

What happens when you modify line #6 and add "return" to the beginning of it?

    RETURN $patterns($array, $temp, $index + 1, $sub['parent']);
John Doe
  • 905
  • 8
  • 9
  • Actually, I see something. Your line "return $temp" isn't inside any function that I can see. It's outside of the $patterns function. Your value of $temp is set at that point - but there's nowhere to "return" it to. Is that it? – John Doe Nov 18 '15 at 03:59