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

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

    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.

提交回复
热议问题