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

前端 未结 23 2131
没有蜡笔的小新
没有蜡笔的小新 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:21

    // $array = your multidimensional array
    
    $flat_array = array();
    
    foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $k=>$v){
    
    $flat_array[$k] = $v;
    
    }
    

    Also documented: http://www.phpro.org/examples/Flatten-Array.html

提交回复
热议问题