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

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

    Use array_walk_recursive

     array());
    
    array_walk_recursive($aNonFlat, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objTmp);
    
    var_dump($objTmp->aFlat);
    
    /*
    array(11) {
      [0]=>
      int(1)
      [1]=>
      int(2)
      [2]=>
      int(3)
      [3]=>
      int(4)
      [4]=>
      int(5)
      [5]=>
      int(6)
      [6]=>
      int(7)
      [7]=>
      int(8)
      [8]=>
      int(9)
      [9]=>
      int(10)
      [10]=>
      int(11)
    }
    */
    
    ?>
    

    Tested with PHP 5.5.9-1ubuntu4.24 (cli) (built: Mar 16 2018 12:32:06)

提交回复
热议问题