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

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

    Sorry for necrobumping, but none of the provided answers did what I intuitively understood as "flattening a multidimensional array". Namely this case:

    [
      'a' => [
        'b' => 'value',
      ]
    ]
    

    all of the provided solutions would flatten it into just ['value'], but that loses information about the key and the depth, plus if you have another 'b' key somewhere else, it will overwrite them.

    I wanted to get a result like this:

    [
      'a_b' => 'value',
    ]
    

    array_walk_recursive doesn't pass the information about the key it's currently recursing, so I did it with just plain recursion:

    function flatten($array, $prefix = '') {
        $return = [];
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $return = array_merge($return, flatten($value, $prefix . $key . '_'));
            } else {
                $return[$prefix . $key] = $value;
            }
        }
        return $return;
    }
    

    Modify the $prefix and '_' separator to your liking.

    Playground here: https://3v4l.org/0B8hf

    0 讨论(0)
  • 2020-11-22 01:20
    /*consider $mArray as multidimensional array and $sArray as single dimensional array
    this code will ignore the parent array
    */
    
    function flatten_array2($mArray) {
        $sArray = array();
    
        foreach ($mArray as $row) {
            if ( !(is_array($row)) ) {
                if($sArray[] = $row){
                }
            } else {
                $sArray = array_merge($sArray,flatten_array2($row));
            }
        }
        return $sArray;
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 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 '<pre>';
    print_r($result);
    echo '</pre>';
    
    0 讨论(0)
  • 2020-11-22 01:25

    Simple approach..See it via recursion..

    <?php
    
    function flatten_array($simple){
    static $outputs=array();
    foreach ( $simple as $value)
    {
    if(is_array($value)){
        flatten_array($value);
    }
    else{
        $outputs[]=$value;
    }
    
    }
    return $outputs;
    }
    
    $eg=['s'=>['p','n'=>['t']]];
    $out=flatten_array($eg);
    print_r($out);
    
    ?>
    
    0 讨论(0)
  • 2020-11-22 01:26

    you can try this:

    function flat_an_array($a)
    {
        foreach($a as $i)
        {
            if(is_array($i)) 
            {
                if($na) $na = array_merge($na,flat_an_array($i));
                else $na = flat_an_array($i);
            }
            else $na[] = $i;
        }
        return $na;
    }
    
    0 讨论(0)
提交回复
热议问题