将多维数组转为一维数组

发布于 2024-12-22 16:54:20 字数 738 浏览 0 评论 0原文

我已经在这个问题上摸索了一段时间了。

我有这个多维数组:

Array
(
    [0] => Array
        (
            [0] => foo
            [1] => bar
            [2] => hello
        )

    [1] => Array
        (
            [0] => world
            [1] => love
        )

    [2] => Array
        (
            [0] => stack
            [1] => overflow
            [2] => yep
            [3] => man
        )

我需要得到这个:

Array
(
    [0] => foo
    [1] => bar
    [2] => hello
    [3] => world
    [4] => love
    [5] => stack
    [6] => overflow
    [7] => yep
    [8] => man
)

有什么想法吗?

我发现的所有其他解决方案都解决了具有不同键的多维数组。我的数组仅使用简单的数字键。

I've been banging my head on this one for a while now.

I have this multidimensional array:

Array
(
    [0] => Array
        (
            [0] => foo
            [1] => bar
            [2] => hello
        )

    [1] => Array
        (
            [0] => world
            [1] => love
        )

    [2] => Array
        (
            [0] => stack
            [1] => overflow
            [2] => yep
            [3] => man
        )

And I need to get this:

Array
(
    [0] => foo
    [1] => bar
    [2] => hello
    [3] => world
    [4] => love
    [5] => stack
    [6] => overflow
    [7] => yep
    [8] => man
)

Any ideas?

All other solutions I found solve multidimensional arrays with different keys. My arrays use simple numeric keys only.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

娇纵 2024-12-29 16:54:20
array_reduce($array, 'array_merge', array())

示例:

$a = array(array(1, 2, 3), array(4, 5, 6));
$result = array_reduce($a, 'array_merge', array());

结果:

array[1, 2, 3, 4, 5, 6];
array_reduce($array, 'array_merge', array())

Example:

$a = array(array(1, 2, 3), array(4, 5, 6));
$result = array_reduce($a, 'array_merge', array());

Result:

array[1, 2, 3, 4, 5, 6];
方圜几里 2024-12-29 16:54:20

PHP array_merge文档函数可以展平你的数组:

$flat = call_user_func_array('array_merge', $array);

如果原始数组的深度高于 2 层,PHP 中的 SPL 有一个 RecursiveArrayIterator 您可以使用它来展平它:

$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0);

另请参阅:如何展平多维数组?

The PHP array_merge­Docs function can flatten your array:

$flat = call_user_func_array('array_merge', $array);

In case the original array has a higher depth than 2 levels, the SPL in PHP has a RecursiveArrayIterator you can use to flatten it:

$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0);

See as well: How to Flatten a Multidimensional Array?.

情域 2024-12-29 16:54:20

从 PHP 5.6 开始这可以是使用 参数更简单地完成拆包

$flat = array_merge(...$array);

As of PHP 5.6 this can be done more simply with argument unpacking.

$flat = array_merge(...$array);
岁月打碎记忆 2024-12-29 16:54:20

这就是它的全部内容:

foreach($array as $subArray){
    foreach($subArray as $val){
        $newArray[] = $val;
    }
}

This is really all there is to it:

foreach($array as $subArray){
    foreach($subArray as $val){
        $newArray[] = $val;
    }
}
秋凉 2024-12-29 16:54:20
foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

其中 $a 是您的数组名称。
了解详情

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

where $a is your array name.
for details

情话难免假 2024-12-29 16:54:20

从 PHP 5.3 开始,最短的解决方案似乎是带有新闭包语法的 array_walk_recursive() :

function flatten(array $array) {
    $return = array();
    array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
    return $return;
}

As of PHP 5.3 the shortest solution seems to be array_walk_recursive() with the new closures syntax:

function flatten(array $array) {
    $return = array();
    array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
    return $return;
}
揪着可爱 2024-12-29 16:54:20

在 PHP5.6 中,还有其他方法来解决这个问题,结合函数,array_shift() 删除原始数组的第一个元素,array_pus() 添加项目新数组,重要的是 of ... slapt/elipse 运算符,它将像参数一样解压 array_shitf() 的返回值。

<?php

$arr = [
        ['foo', 'bar', 'hello'],
        ['world', 'love'],
        ['stack', 'overflow', 'yep', 'man', 'wow']
    ];

$new = [];
while($item = array_shift($arr)){
    array_push($new, ...$item);
}

print_r($new);

输出:

Array
(
    [0] => foo
    [1] => bar
    [2] => hello
    [3] => world
    [4] => love
    [5] => stack
    [6] => overflow
    [7] => yep
    [8] => man
    [9] => wow
)

示例 - ideone

In PHP5.6 there other way to solve this problem, combining the functions, array_shift() to remove the first elemente of the original array, array_pus() to add items an new array, the important thing is the of ... splapt/elipse operator it will unpack the return of array_shitf() like an argument.

<?php

$arr = [
        ['foo', 'bar', 'hello'],
        ['world', 'love'],
        ['stack', 'overflow', 'yep', 'man', 'wow']
    ];

$new = [];
while($item = array_shift($arr)){
    array_push($new, ...$item);
}

print_r($new);

Output:

Array
(
    [0] => foo
    [1] => bar
    [2] => hello
    [3] => world
    [4] => love
    [5] => stack
    [6] => overflow
    [7] => yep
    [8] => man
    [9] => wow
)

Example - ideone

兔姬 2024-12-29 16:54:20

我曾使用此代码来解决相同类型的问题。所以你也可以尝试这个。

function array_flatten($array) { 

 if (!is_array($array))  
{ 
  return FALSE;  
}  
  $result = array(); 
foreach ($array as $key => $value)
{
  if (is_array($value))  
  {
   $result = array_merge($result, array_flatten($value));
  } 
  else  {
  $result[$key] = $value;   
  }  
}   
return $result; 
} 

I had used this code to resolve same type of problem. so you can also try this.

function array_flatten($array) { 

 if (!is_array($array))  
{ 
  return FALSE;  
}  
  $result = array(); 
foreach ($array as $key => $value)
{
  if (is_array($value))  
  {
   $result = array_merge($result, array_flatten($value));
  } 
  else  {
  $result[$key] = $value;   
  }  
}   
return $result; 
} 
坏尐絯℡ 2024-12-29 16:54:20

这将使

array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });

This will make

array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
甜是你 2024-12-29 16:54:20
$blocked_dates = array(
                    '2014' => Array
                        (
                            '8' => Array
                                (
                                    '3' => '1',
                                    '4' => '1',                                     
                                    '6' => '1',                                     
                                    '10' => '1',
                                    '15' => '1',
                                    '25' => '1'

                                )

                        ),
                    '2015' => Array
                        (
                            '9' => Array
                                (
                                    '3' => '1',
                                    '4' => '1',                                    
                                    '6' => '1',                                     
                                    '10' => '1',
                                    '15' => '1',
                                    '25' => '1'

                                )

                        )    

                );

结果(一维数组):

$unavailable_dates = array();
foreach ($blocked_dates as $year=>$months) {

    foreach ($months as $month => $days) {

        foreach ($days as $day => $value) {

            array_push($unavailable_dates,"$year-$month-$day");
        }

    }
}



$unavailable_dates = json_encode($unavailable_dates);
print_r($unavailable_dates);

OUTPUT : ["2014-8-3","2014-8-4","2014-8-6","2014-8-10","2014-8-15","2014-8-25","2015-9-3","2015-9-4","2015-9-6","2015-9-10","2015-9-15","2015-9-25"]
$blocked_dates = array(
                    '2014' => Array
                        (
                            '8' => Array
                                (
                                    '3' => '1',
                                    '4' => '1',                                     
                                    '6' => '1',                                     
                                    '10' => '1',
                                    '15' => '1',
                                    '25' => '1'

                                )

                        ),
                    '2015' => Array
                        (
                            '9' => Array
                                (
                                    '3' => '1',
                                    '4' => '1',                                    
                                    '6' => '1',                                     
                                    '10' => '1',
                                    '15' => '1',
                                    '25' => '1'

                                )

                        )    

                );

RESUTL(ONE DIMENTIONAL ARRAY) :

$unavailable_dates = array();
foreach ($blocked_dates as $year=>$months) {

    foreach ($months as $month => $days) {

        foreach ($days as $day => $value) {

            array_push($unavailable_dates,"$year-$month-$day");
        }

    }
}



$unavailable_dates = json_encode($unavailable_dates);
print_r($unavailable_dates);

OUTPUT : ["2014-8-3","2014-8-4","2014-8-6","2014-8-10","2014-8-15","2014-8-25","2015-9-3","2015-9-4","2015-9-6","2015-9-10","2015-9-15","2015-9-25"]
跨年 2024-12-29 16:54:20

最快的解决方案是使用这个 数组库

$flattened = Arr::flatten($yourArray);

它将生成您想要的数组

The quickest solution would be to use this array library:

$flattened = Arr::flatten($yourArray);

which will produce exactly the array you want

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文