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

前端 未结 23 2112
没有蜡笔的小新
没有蜡笔的小新 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;
    }
    
    0 讨论(0)
  • 2020-11-22 01:15

    A non-recursive solution (but order-destroying):

    function flatten($ar) {
        $toflat = array($ar);
        $res = array();
    
        while (($r = array_shift($toflat)) !== NULL) {
            foreach ($r as $v) {
                if (is_array($v)) {
                    $toflat[] = $v;
                } else {
                    $res[] = $v;
                }
            }
        }
    
        return $res;
    }
    
    0 讨论(0)
  • 2020-11-22 01:16
    function flatten_array($array, $preserve_keys = 0, &$out = array()) {
        # Flatten a multidimensional array to one dimension, optionally preserving keys.
        #
        # $array - the array to flatten
        # $preserve_keys - 0 (default) to not preserve keys, 1 to preserve string keys only, 2 to preserve all keys
        # $out - internal use argument for recursion
        foreach($array as $key => $child)
            if(is_array($child))
                $out = flatten_array($child, $preserve_keys, $out);
            elseif($preserve_keys + is_string($key) > 1)
                $out[$key] = $child;
            else
                $out[] = $child;
        return $out;
    }
    
    0 讨论(0)
  • 2020-11-22 01:16

    I found a simple way to convert multilevel array into one. I use the function "http_build_query" which converts the array into a url string. Then, split the string with explode and decode the value.

    Here is a sample.

    $converted = http_build_query($data);
    $rows = explode('&', $converted);
    $output = array();
    foreach($rows AS $k => $v){
       list($kk, $vv) = explode('=', $v);
       $output[ urldecode($kk) ] =  urldecode($vv);
    }
    return $output;
    
    0 讨论(0)
  • 2020-11-22 01:17
    $array  = your array
    
    $result = call_user_func_array('array_merge', $array);
    
    echo "<pre>";
    print_r($result);
    

    REF: http://php.net/manual/en/function.call-user-func-array.php

    Here is another solution (works with multi-dimensional array) :

    function array_flatten($array) {
    
       $return = array();
       foreach ($array as $key => $value) {
           if (is_array($value)){ $return = array_merge($return, array_flatten($value));}
           else {$return[$key] = $value;}
       }
       return $return;
    
    }
    
    $array  = Your array
    
    $result = array_flatten($array);
    
    echo "<pre>";
    print_r($result);
    
    0 讨论(0)
  • 2020-11-22 01:17

    If you specifically have an array of arrays that doesn't go further than one level deep (a use case I find common) you can get away with array_merge and the splat operator.

    <?php
    
    $notFlat = [[1,2],[3,4]];
    $flat = array_merge(...$notFlat);
    var_dump($flat);
    

    Output:

    array(4) {
      [0]=>
      int(1)
      [1]=>
      int(2)
      [2]=>
      int(3)
      [3]=>
      int(4)
    }
    

    The splat operator effectively changes the array of arrays to a list of arrays as arguments for array_merge.

    0 讨论(0)
提交回复
热议问题