Convert multidimensional array into single array

后端 未结 20 1560
时光取名叫无心
时光取名叫无心 2020-11-22 10:39

I have an array which is multidimensional for no reason

/* This is how my array is currently */
Array
(
[0] => Array
    (
        [0] => Array
                


        
20条回答
  •  北海茫月
    2020-11-22 11:15

    Following this pattern

    $input = array(10, 20, array(30, 40), array('key1' => '50', 'key2'=>array(60), 70));
    

    Call the function :

    echo "
    ";print_r(flatten_array($input, $output=null));
    

    Function Declaration :

    function flatten_array($input, $output=null) {
    if($input == null) return null;
    if($output == null) $output = array();
    foreach($input as $value) {
        if(is_array($value)) {
            $output = flatten_array($value, $output);
        } else {
            array_push($output, $value);
        }
    }
    return $output;
    

    }

提交回复
热议问题