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

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

    Given multi-dimensional array and converting it into one-dimensional, can be done by unsetting all values which are having arrays and saving them into first dimension, for example:

    function _flatten_array($arr) {
      while ($arr) {
        list($key, $value) = each($arr); 
        is_array($value) ? $arr = $value : $out[$key] = $value;
        unset($arr[$key]);
      }
      return (array)$out;
    }
    

提交回复
热议问题