如何将多维数组转换为一级数组?

问题描述 投票:-1回答:1

我有一个多维数组,但我想将所有值放入一个数组中:

array:28 [▼
  0 => array:8 [▼
    0 => "id"
    1 => "monkey"
    2 => "horse"
    3 => "bird"
    4 => "elephant"
    5 => "cat"
    6 => "whale"
    7 => "frog"
  ]
  1 => array:4 [▼
    0 => "id"
    1 => "whale"
    2 => "lion"
    3 => "dog"
  ]
  2 => array:4 [▶]
  3 => array:4 [▶]
  4 => array:3 [▶]
  5 => array:4 [▶]
  6 => array:10 [▶]
  7 => array:3 [▶]
  8 => array:5 [▶]
  9 => array:18 [▶]
  10 => array:3 [▶]
  11 => array:4 [▶]
  12 => array:5 [▶]
  13 => array:3 [▶]
  14 => array:6 [▶]
  15 => array:3 [▶]
  16 => array:3 [▶]
  17 => array:3 [▶]
  18 => array:3 [▶]
  19 => array:4 [▶]
  20 => array:6 [▶]
  21 => array:3 [▶]
  22 => array:5 [▶]
  23 => array:8 [▶]
  24 => array:3 [▶]
  25 => array:5 [▶]
  26 => array:3 [▶]
  27 => array:5 [▶]
]

我的方法:

  $singleArray = array();
  foreach ($multiArray as $key => $value){
    $singleArray[] =$value;
    array_merge($singleArray, $value);
  }
  dump($singleArray);

但是这再次创建了多维数组

php arrays multidimensional-array
1个回答
0
投票

似乎您希望所有子数组值都在单个数组中。

$singleArray = [];
foreach($multiarray as $array) {
    $singleArray = array_merge($singleArray, array_values($array));
}
© www.soinside.com 2019 - 2024. All rights reserved.