How to “flatten” a multi-dimensional array to simple one in PHP?

前端 未结 23 2154
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 01:03

It\'s probably beginner question but I\'m going through documentation for longer time already and I can\'t find any solution. I thought I could use implode for each dimensio

23条回答
  •  别那么骄傲
    2020-11-22 01:23

    any of this didnt work for me ... so had to run it myself. works just fine:

    function arrayFlat($arr){
    $out = '';
        foreach($arr as $key => $value){
    
            if(!is_array($value)){
                $out .= $value.',';
            }else{
                $out .= $key.',';
                $out .= arrayFlat($value);
            }
    
        }
        return trim($out,',');
    }
    
    
    $result = explode(',',arrayFlat($yourArray));
    echo '
    ';
    print_r($result);
    echo '
    ';

提交回复
热议问题