多维数组到单个数组

时间:2016-02-15 14:44:30

标签: php arrays

是否有PHP数组函数来压缩以下数组结构;

array:2 [▼
  0 => array:2 [▼
    0 => {#90 ▶}
    1 => {#91 ▶}
  ]
  1 => array:5 [▼
    0 => {#92 ▶}
    1 => {#93 ▶}
    2 => {#94 ▶}
    3 => {#95 ▶}
    4 => {#96 ▶}
  ]
]

在这样的事情上;

array:7 [▼
    0 => {#90 ▶}
    1 => {#91 ▶}
    3 => {#92 ▶}
    4 => {#93 ▶}
    5 => {#94 ▶}
    6 => {#95 ▶}
    7 => {#96 ▶}
  ]
]

对不起,这是使用Laravel的dd(死亡和转储),但我想你会对我想要实现的目标有一个大概的了解。

另请注意,这可能并不总是传入数据的结构。有时它只是一个单维数组。所以这会导致错误;

$domain->questions = call_user_func_array('array_merge', $domain->questions);

5 个答案:

答案 0 :(得分:1)

这似乎有效,但只有在阵列深度为2级时才会起作用。幸运的是,这就是我现在所需要的一切;

if(is_array($questions[0])) {
    $questions = call_user_func_array('array_merge', $questions);
}

答案 1 :(得分:0)

这是一维和二维数组的解决方案:

<?php
$input = array(
    array(
        0 => '#90',
        1 => '#91',
    ),
    array(
        0 => '#92',
        1 => '#93',
        2 => '#94',
        3 => '#95',
        4 => '#96',
    )
);

$newarray = array();
foreach($input as $items) {
    if (is_array($items)) foreach ($items as $item) {
        $newarray[] = $item;
    } else {
        $newarray[] = $items;
    }
}

echo '<pre>'; print_r($newarray); echo '</pre>';

这是带有混合数组(数组数组和项数组)的 foreach 逻辑:

<?php

$input = array(
    0 => '#90',
    array(
        0 => '#91',
        1 => '#92',
    ),
    4 => '#93',
    5 => '#94',
    6 => '#95',
    7 => '#96',
);

$newarray = array();
foreach($input as $items) {
    if (is_array($items)) foreach ($items as $item) {
        $newarray[] = $item;
    } else {
        $newarray[] = $items;
    }
}
echo '<pre>'; print_r($newarray); echo '</pre>';

因此无论输入数组是一维的,二维的还是混合的, foreach 逻辑都是可用的。

两种情况下的结果都是:

  

阵列
  (
      [0] =&gt; #90
      [1] =&gt; #91
      [2] =&gt; #92
      [3] =&gt; #93
      [4] =&gt; #94
      [5] =&gt; #95
      [6] =&gt; #96
  )

答案 2 :(得分:0)

因为您不知道数组是否包含子项,您可以尝试这样的事情:

function flatten($array) {
    // If this is already an empty array or a flat array, just return it
    if (empty($array) || ! is_array($array[0])) {
        return $array;
    }

    // just merge all sub-arrays into a single array
    $flat = [];
    foreach ($array as $item) {
        $flat = array_merge($flat, $item)
    }
    return $flat;
}

另一种选择会更慢(但对不同格式更具弹性):

function flatten($array) {
    // If this is already an empty array just return it
    if (empty($array)) {
        return $array;
    }

    // just add all sub-items into a single array
    $flat = [];
    foreach ($array as $item) {
        if (is_array($item)) {
           $flat = array_merge($flat, flatten($item));
        } else {
           $flat[] = $item;
        }
    }
    return $flat;
}

这将对阵列进行“深度”展平,但正如我所说 - 会慢一些

答案 3 :(得分:0)

尝试这个简单的功能:

function flatArray( $array )
{
    $retval = array();

    foreach( $array as $val )
    {
        if( is_array( $val ) ) $retval  = array_merge( $retval, flatArray( $val ) );
        else                   $retval[] = $val;
    }
    return $retval;
}

然后:

$array1 = [['#90','#91'],['#92','#93','#94','#95','#96']];
$array2 = ['#82','#83','#84','#85','#86'];
$array3 = [['#90','#91',['100','101']],['#92','#93','#94','#95','#96']];
print_r( flatArray( $array1 ) );
print_r( flatArray( $array2 ) );
print_r( flatArray( $array3 ) );

将输出:

Array
(
    [0] => #90
    [1] => #91
    [2] => #92
    [3] => #93
    [4] => #94
    [5] => #95
    [6] => #96
)

Array
(
    [0] => #82
    [1] => #83
    [2] => #84
    [3] => #85
    [4] => #86
)

和 - 对于array3(三个级别):

Array
(
    [0] => #90
    [1] => #91
    [2] => 100
    [3] => 101
    [4] => #92
    [5] => #93
    [6] => #94
    [7] => #95
    [8] => #96
)

eval.in demo

编辑:

添加了递归调用:通过这种方式,您甚至可以平放多嵌套数组

答案 4 :(得分:0)

我认为这可以做你想要的事情

<?php

$tester = [
    ['90', '91'],
    ['92', '93', '94', '95', '96']
];

$tester2 = array_merge($tester[0], $tester[1]);

print_r($tester);
print_r($tester2);

生成此输出:

Array
(
    [0] => Array
        (
            [0] => 90
            [1] => 91
        )

    [1] => Array
        (
            [0] => 92
            [1] => 93
            [2] => 94
            [3] => 95
            [4] => 96
        )

)
Array
(
    [0] => 90
    [1] => 91
    [2] => 92
    [3] => 93
    [4] => 94
    [5] => 95
    [6] => 96
)