douan4347
2015-12-29 20:15
浏览 17
已采纳

PHP在一维数组数组中转换多维数组

I have array of 15 elements and each element contains 1000 arrays. How can I convert all this arrays in one simple dimensional array? I need to get this inner arrays and convert this in one dimensional array of arrays.

    Array
(
    [0] => Array
        (
            [0] => Array[0..999]
            [1] => Array[0..999]
            [2] => Array[0..999]
        )

    [1] => Array
        (
            [0] => Array[0..999]///items are also arrays
            [1] => Array[0..999]
        )

    [2] => Array
        (

            [0] => Array[0..999]
            [1] => Array[0..999]
            [2] => Array[0..999]
        )
....

It needs to look like this:

 Array(
[0] => Array
    [0]=>value
    [1]=>value
    [2]=>value

[1] => Array
    [0]=>value
    [1]=>value
    [2]=>value
)

图片转代码服务由CSDN问答提供 功能建议

我有15个元素的数组,每个元素包含1000个数组。 如何在一个简单的维数组中转换所有这些数组? 我需要得到这个内部数组并将其转换为一维数组数组。

  Array 
(
 [0] =>数组
(
 [  0] =>数组[0..999] 
 [1] =>数组[0..999] 
 [2] =>数组[0..999] 
)
 
 [  1] =>数组
(
 [0] =>数组[0..999] ///项目也是数组
 [1] =>数组[0..999] 
)\  n 
 [2] =>数组
(
 
 [0] =>数组[0..999] 
 [1] =>数组[0..999] 
 [2]  =>数组[0..999] 
)
 .... 
   
 
 

它需要如下所示: \ n

  Array(
 [0] => Array 
 [0] => value 
 [1] => value 
 [2] => value 
 
 [  1] =>数组
 [0] =>值
 [1] =>值
 [2] =>值
)
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongse5408 2015-12-29 20:26
    已采纳

    array_merge() will merge multiple arrays, but you have an unspecified number in the main array. So use the array as the parameters for call_user_func_array() with array_merge() as the callback:

    $result = call_user_func_array('array_merge', $array);
    
    已采纳该答案
    打赏 评论
  • drgzmmy6379 2015-12-29 20:24

    Create an empty 'main' array and merge the 15 arrays into it

    $main_array = [];
    
    foreach($big_array as $value)
    {
        $main_array = array_merge($main_array,$value);
    }
    

    e.g:

    $main_array = [];
    foreach([[1],[2],[3,4,5,6,7]] as $value)
    {
        $main_array = array_merge($main_array,$value);
    }
    print_r($main_array);
    
    //output
    Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
        [4] => 5
        [5] => 6
        [6] => 7
    )
    
    打赏 评论
  • duancanjiu3754 2015-12-29 20:44

    A recursive function, one that calls itself, could do the trick, e.g.

    <?php
    
    $currentArray = [*your multi-dim array*];
    $targetArray = [];
    
    $level = 0;
    
    $targetArray = flattenArray($currentArray, $targetArray, $level);
    
    function flattenArray($currentArray, $targetArray, $level){
      foreach($currentArray as $value){
        if(is_array($value) AND $level < 1){
          $targetArray = flattenArray($value, $targetArray, $level + 1);
        }else{
          $targetArray[] = array(print_r($value, TRUE));
        }
      }
      return $targetArray;
    }
    
    ?>
    
    打赏 评论

相关推荐 更多相似问题