如何在PHP中将二维数组转换为一维数组?

时间:2022-01-10 21:36:32

I am Having an mutidimensional array getting result like given below

我有一个多维数组得到如下给出的结果

Array
(
    [0] => Array
        (
            [0] => 70
        )

    [1] => Array
        (
            [0] => 67
        )

    [2] => Array
        (
            [0] => 75
            [1] => 73
            [2] => 68
        )

    [3] => Array
        (
            [0] => 68
        )

    [4] => Array
        (
            [0] => 76
        )

)

But I need to convert it to single array

但我需要将其转换为单个数组

And I want to convert in to single dimensional array as

我想转换为单维数组

Array
(
[0] => 70
[1] => 67
[2] => 75
[3] => 73
[4] => 68
[5] => 68
[6] => 76
)

How to convert it using php functions?

如何使用PHP函数转换它?

Or Is there any other way to do it?

或者还有其他办法吗?

6 个解决方案

#1


15  

You can try

你可以试试

$it =  new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$l = iterator_to_array($it, false);

var_dump($l); // one Dimensional 

#2


2  

Try with:

$input  = array(/* your array*/);
$output = array();

foreach ( $input as $data ) {
  $output = array_merge($output, $data);
}

#3


1  

You can use array_walk_recursive() for that coupled with a closure:

你可以使用array_walk_recursive()和一个闭包:

$res = array(); // initialize result

// apply closure to all items in $data
array_walk_recursive($data, function($item) use (&$res) {
    // flatten the array
    $res[] = $item;
});

print_r($res); // print one-dimensional array

#4


0  

This should do the trick

这应该可以解决问题

$final = array();
foreach ($outer as $inner) {
  $final = array_merge($final, $inner);
}
var_dump($final);

Or you could use array_reduce() if you have PHP >= 5.3

或者,如果PHP> = 5.3,则可以使用array_reduce()

$final = array_reduce($outer, function($_, $inner){
  return $_ = array_merge((array)$_, $inner);
});
var_dump($final);

#5


0  

For a more generic function which can deal with multidimensional arrays, check this function,

对于可以处理多维数组的更通用的函数,请检查此函数,

function  arrayFlattener($input = array(), &$output = array()) {
  foreach($input as $value) {
    if(is_array($value)) {
        arrayFlattener($value, $output);
    } else {
        $output[] = $value;
    }
  }
}

You can find an example here.

你可以在这里找到一个例子。

#6


0  

By using this function you can convert any dimension array into a single dimention array.

通过使用此函数,您可以将任何维数组转换为单维数组。

$result = array();
$data = mearchIntoarray($result,$array);
function mearchIntoarray($result,$now)
{
    global $result;
    foreach($now as $key=>$val)
    {
        if(is_array($val))
        {
            mearchIntoarray($result,$val);
        }
        else
        {
            $result[] = $val;
        }
    }
    return $result;
} 

Where $array is your given array value.

其中$ array是给定的数组值。

#1


15  

You can try

你可以试试

$it =  new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$l = iterator_to_array($it, false);

var_dump($l); // one Dimensional 

#2


2  

Try with:

$input  = array(/* your array*/);
$output = array();

foreach ( $input as $data ) {
  $output = array_merge($output, $data);
}

#3


1  

You can use array_walk_recursive() for that coupled with a closure:

你可以使用array_walk_recursive()和一个闭包:

$res = array(); // initialize result

// apply closure to all items in $data
array_walk_recursive($data, function($item) use (&$res) {
    // flatten the array
    $res[] = $item;
});

print_r($res); // print one-dimensional array

#4


0  

This should do the trick

这应该可以解决问题

$final = array();
foreach ($outer as $inner) {
  $final = array_merge($final, $inner);
}
var_dump($final);

Or you could use array_reduce() if you have PHP >= 5.3

或者,如果PHP> = 5.3,则可以使用array_reduce()

$final = array_reduce($outer, function($_, $inner){
  return $_ = array_merge((array)$_, $inner);
});
var_dump($final);

#5


0  

For a more generic function which can deal with multidimensional arrays, check this function,

对于可以处理多维数组的更通用的函数,请检查此函数,

function  arrayFlattener($input = array(), &$output = array()) {
  foreach($input as $value) {
    if(is_array($value)) {
        arrayFlattener($value, $output);
    } else {
        $output[] = $value;
    }
  }
}

You can find an example here.

你可以在这里找到一个例子。

#6


0  

By using this function you can convert any dimension array into a single dimention array.

通过使用此函数,您可以将任何维数组转换为单维数组。

$result = array();
$data = mearchIntoarray($result,$array);
function mearchIntoarray($result,$now)
{
    global $result;
    foreach($now as $key=>$val)
    {
        if(is_array($val))
        {
            mearchIntoarray($result,$val);
        }
        else
        {
            $result[] = $val;
        }
    }
    return $result;
} 

Where $array is your given array value.

其中$ array是给定的数组值。