-1

I am trying to filter set of array through recursive function but can not return it when filtered is done.

function get_the_answer($number, $array=array()){
 $returnArray = array();
 $QS = array(1=>'C', 2=>'B', 3=>'C', 4=>'D');
 $SL = array(
           1 => array(
                    'cb1' => array('A','B', 'D'),
                    'cb2' => array('B','C', 'D'),
                    'cb3' => array('A','C', 'B'),
                ),

            2 => array(
                    'cb1' => array('B','C','D'),
                    'cb2' => array('A','C','D'),
                    'cb3' => array('A','B','D'),
                ),

            3 => array(
                    'cb1' => array('E','C','D'),
                    'cb2' => array('B','E','D'),
                    'cb3' => array('A','C','D'),
                ),
            4 => array(
                    'cb1' => array('A','D','E'),
                    'cb2' => array('A','C','D'),
                    'cb3' => array('B','C','D'),
            )
 ); 
 if($number<=4){ 
        if(empty($array)){
          $returnedValue = return_filtered_company($SL[$number], $QS[$number]);
        } else {
          $returnedValue = return_filtered_company($array, $QS[$number]);
        }

        //print_r($returnedValue); -> receiving values nicely, need the last set of array which is 'cb3'

        $returnArray[] = $returnedValue; 

        $increment = $number+1;
        if($increment<=4){
            $result=array_intersect_key($SL[$increment], $returnedValue);
             get_the_answer($increment, $result);
        }
    }
  return $returnArray;
}

//function for company calculation 
function return_filtered_company($companyArray, $answer){
 $foundCB=array();

 if(is_array($companyArray) && count($companyArray)){
    foreach ($companyArray as $name => $arrayVal){
        if (in_array($answer, $arrayVal)) {
            $foundCB[$name]=$name;
        }
    }
 }

return $foundCB;
}

I am calling answer function like this way:

$ans = get_the_answer(1);
    echo '<pre>';
    print_r($ans);
    echo '</pre>';

Everything is working fine but it is returning value which happened in 1st recursion not the last one. Answer should be cb3

Someone please help to find out what is/are the best solution to return last occurrence of recursion.

Mehedi Hasan
  • 77
  • 1
  • 9
  • 2
    If you call a recursive function, you should only work with it's return value. Currently you only have a recursive call, but without usage of the return value – Philipp Aug 05 '18 at 13:45
  • are you sure you want a recoursive function and not just a loop? (tbh I don't get what you want to do here) – Jeff Aug 05 '18 at 14:01
  • Hi @Philipp thanks for your reply :) What can be the best solution if i want to get the result that i mentioned above ? – Mehedi Hasan Aug 05 '18 at 14:01
  • @Jeff it is not necessary to run recursive function but i thought it would be the easiest way to get solution :( Goal is to filter $SL array based on $QS value. This is actually a question-answer solution that i want to filter. – Mehedi Hasan Aug 05 '18 at 14:04

1 Answers1

1

Replacing line 42 get_the_answer($increment, $result); with return get_the_answer($increment, $result); solves it.

See the screenshot

Pol_pm
  • 65
  • 6