0
Array
(
    [340] => Array(
            [0] => 341
        )
    [228] => Array(
            [0] => 234
            [1] => 239
        )
    [343] => Array (
            [0] => 344
            [1] => 345
        )
)

desired out put

array(
[0] => 340
[1] => 341
[2] => 228
[3] => 234
[4] => 239
[5] => 343
[6] => 344
[7] => 345
)

I am trying to use recursive function to get the output I have tried with a php code but not able to get desired output

$simple_array = get_exam_preference_list_array($list);
function get_exam_preference_list_array($list, $list_array = array()){
            foreach($list as $key=>$pref_list){
                $list_array[]  = $key;  
                if(is_array($pref_list)){

                    get_exam_preference_list_array(array_flip($pref_list), $list_array);
                }
            }
            return $list_array;
}

Please help

2 Answers 2

1

You forget to merge the recursive call function

Codesandbox Demo

function get_exam_preference_list_array($list, $list_array = array()){
            foreach($list as $key=>$pref_list){
                $list_array[]  = $key; 
                if(is_array($pref_list)){
                   $list_array = array_merge(get_exam_preference_list_array(array_flip($pref_list)),$list_array);
                }
            }
            return $list_array;
}
Sign up to request clarification or add additional context in comments.

Comments

0
$input_array;           // This is your input array
$output_array = [];     // This is where your output will be stored.
foreach ($input_array as $k => $v)
{
array_push($output_array, $v);
}
print_r($output_array);

1 Comment

Code only answers can almost always be improved by adding some explanation of how and why they work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.