Turning multidimensional array into one-dimensiona

2019-01-01 04:16发布

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.

10条回答
只若初见
2楼-- · 2019-01-01 04:23

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

查看更多
还给你的自由
3楼-- · 2019-01-01 04:23

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; 
} 
查看更多
流年柔荑漫光年
4楼-- · 2019-01-01 04:27

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?.

查看更多
怪性笑人.
5楼-- · 2019-01-01 04:36

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;
}
查看更多
谁念西风独自凉
6楼-- · 2019-01-01 04:36

This will make

array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
查看更多
旧时光的记忆
7楼-- · 2019-01-01 04:42
$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"]
查看更多
登录 后发表回答