将多维数组转换为单个数组

时间:2021-11-07 07:28:55

I have an array which is multidimensional for no reason

我有一个无理由的多维数组

/* This is how my array is currently */
Array
(
[0] => Array
    (
        [0] => Array
            (
                [plan] => basic
            )

        [1] => Array
            (
                [plan] => small
            )

        [2] => Array
            (
                [plan] => novice
            )

        [3] => Array
            (
                [plan] => professional
            )

        [4] => Array
            (
                [plan] => master
            )

        [5] => Array
            (
                [plan] => promo
            )

        [6] => Array
            (
                [plan] => newplan
            )

    )

 )

I want to convert this array into this form

我想把这个数组转换成这个形式。

/*Now, I want to simply it down to this*/
Array (
[0] => basic
[1] => small
[2] => novice
[3] => professional
[4] => master
[5] => promo
[6] => newplan
)

Any idea how to do this?

你知道怎么做吗?

11 个解决方案

#1


71  

Assuming this array may or may not be redundantly nested and you're unsure of how deep it goes, this should flatten it for you:

假设这个数组可能是冗余的,也可能不是冗余的,并且您不确定它有多深,这应该会使它更平坦:

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; 
} 

#2


50  

$array = array_column($array, 'plan');

The first argument is an array | The second argument is array key.

第一个参数是数组|第二个参数是数组键。

Note: array_column was introduced in PHP 5.5 so it won't work in earlier versions.

注意:array_column是在PHP 5.5中引入的,因此在早期版本中不能使用。

#3


13  

Just assign it to it's own first element:

把它分配给它自己的第一个元素:

$array = $array[0];

#4


7  

 $singleArray = array();

    foreach ($multiDimensionalArray as $key => $value){
        $singleArray[$key] = $value['plan'];
    }

this is best way to create a array from multiDimensionalArray array.

这是从多维数组创建数组的最佳方式。

thanks

谢谢

#5


4  

For this particular case, this'll do:

对于这个特殊的案例,它可以:

$array = array_map('current', $array[0]);

It's basically the exact same question is this one, look at some answers there: PHP array merge from unknown number of parameters.

这基本上是一个完全相同的问题,看看这里的一些答案:PHP数组从未知参数中合并。

#6


2  

You can do it just using a loop.

你可以只使用一个循环。

    $singleArray = array();

    foreach ($multiDimensionalArray as $key => $value){
        $singleArray[$key] = $value['plan'];
    }

#7


2  

Recently I've been using AlienWebguy's array_flatten function but it gave me a problem that was very hard to find the cause of.
array_merge causes problems, and this isn't the first time that I've made problems with it either.
If you have the same array keys in one inner array that you do in another, then the later values will overwrite the previous ones in the merged array.

最近我一直在使用AlienWebguy的array_flatten函数,但它给了我一个很难找到原因的问题。array_merge会导致问题,这也不是我第一次遇到问题。如果在一个内部数组中有相同的数组键,那么后面的值将覆盖合并数组中的前一个。

Here's a different version of array_flatten without using array_merge:

下面是不使用array_merge的array_flatten的另一个版本:

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

#8


0  

I have done this with OOP style

我使用了OOP风格

$res=[1=>[2,3,7,8,19],3=>[4,12],2=>[5,9],5=>6,7=>[10,13],10=>[11,18],8=>[14,20],12=>15,6=>[16,17]];
class MultiToSingle{
public $result=[];
public function __construct($array){
    if(!is_array($array)){
        echo "Give a array";
    }
    foreach($array as $key => $value){
        if(is_array($value)){
            for($i=0;$i<count($value);$i++){
                $this->result[]=$value[$i];
            }  
        }else{
            $this->result[]=$value;
        }
    }
}
}

$obj= new MultiToSingle($res);
$array=$obj->result;
print_r($array);

#9


0  

Multi dimensional array to single array with one line code !!! Enjoy the code.

多维数组到单数组与一行代码!!!享受的代码。

$array=[1=>[2,5=>[4,2],[7,8=>[3,6]],5],4];
$arr=[];
array_walk_recursive($array, function($k){global $arr; $arr[]=$k;});
print_r($arr);

...Enjoy the code.

…享受的代码。

#10


0  

$flattenArray = [];

foreach ($parentArray as $childArray) {
    foreach ($childArray as $value) {
        $flattenArray[] = $value;
    }
}

#11


0  

Following this pattern

在这个模式

$input = array(10, 20, array(30, 40), array('key1' => '50', 'key2'=>array(60), 70));

Call the function :

调用函数:

echo "<pre>";print_r(flatten_array($input, $output=null));

Function Declaration :

函数声明:

function flatten_array($input, $output=null) {
    if($input == null) return null;
    if($output == null) $output = array();

    foreach($input as $value) {
        if(is_array($value)) {
            $output = flatten_array($value, $output);
        } else {
            array_push($output, $value);
        }
    }

    return $output;
}

#1


71  

Assuming this array may or may not be redundantly nested and you're unsure of how deep it goes, this should flatten it for you:

假设这个数组可能是冗余的,也可能不是冗余的,并且您不确定它有多深,这应该会使它更平坦:

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; 
} 

#2


50  

$array = array_column($array, 'plan');

The first argument is an array | The second argument is array key.

第一个参数是数组|第二个参数是数组键。

Note: array_column was introduced in PHP 5.5 so it won't work in earlier versions.

注意:array_column是在PHP 5.5中引入的,因此在早期版本中不能使用。

#3


13  

Just assign it to it's own first element:

把它分配给它自己的第一个元素:

$array = $array[0];

#4


7  

 $singleArray = array();

    foreach ($multiDimensionalArray as $key => $value){
        $singleArray[$key] = $value['plan'];
    }

this is best way to create a array from multiDimensionalArray array.

这是从多维数组创建数组的最佳方式。

thanks

谢谢

#5


4  

For this particular case, this'll do:

对于这个特殊的案例,它可以:

$array = array_map('current', $array[0]);

It's basically the exact same question is this one, look at some answers there: PHP array merge from unknown number of parameters.

这基本上是一个完全相同的问题,看看这里的一些答案:PHP数组从未知参数中合并。

#6


2  

You can do it just using a loop.

你可以只使用一个循环。

    $singleArray = array();

    foreach ($multiDimensionalArray as $key => $value){
        $singleArray[$key] = $value['plan'];
    }

#7


2  

Recently I've been using AlienWebguy's array_flatten function but it gave me a problem that was very hard to find the cause of.
array_merge causes problems, and this isn't the first time that I've made problems with it either.
If you have the same array keys in one inner array that you do in another, then the later values will overwrite the previous ones in the merged array.

最近我一直在使用AlienWebguy的array_flatten函数,但它给了我一个很难找到原因的问题。array_merge会导致问题,这也不是我第一次遇到问题。如果在一个内部数组中有相同的数组键,那么后面的值将覆盖合并数组中的前一个。

Here's a different version of array_flatten without using array_merge:

下面是不使用array_merge的array_flatten的另一个版本:

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

#8


0  

I have done this with OOP style

我使用了OOP风格

$res=[1=>[2,3,7,8,19],3=>[4,12],2=>[5,9],5=>6,7=>[10,13],10=>[11,18],8=>[14,20],12=>15,6=>[16,17]];
class MultiToSingle{
public $result=[];
public function __construct($array){
    if(!is_array($array)){
        echo "Give a array";
    }
    foreach($array as $key => $value){
        if(is_array($value)){
            for($i=0;$i<count($value);$i++){
                $this->result[]=$value[$i];
            }  
        }else{
            $this->result[]=$value;
        }
    }
}
}

$obj= new MultiToSingle($res);
$array=$obj->result;
print_r($array);

#9


0  

Multi dimensional array to single array with one line code !!! Enjoy the code.

多维数组到单数组与一行代码!!!享受的代码。

$array=[1=>[2,5=>[4,2],[7,8=>[3,6]],5],4];
$arr=[];
array_walk_recursive($array, function($k){global $arr; $arr[]=$k;});
print_r($arr);

...Enjoy the code.

…享受的代码。

#10


0  

$flattenArray = [];

foreach ($parentArray as $childArray) {
    foreach ($childArray as $value) {
        $flattenArray[] = $value;
    }
}

#11


0  

Following this pattern

在这个模式

$input = array(10, 20, array(30, 40), array('key1' => '50', 'key2'=>array(60), 70));

Call the function :

调用函数:

echo "<pre>";print_r(flatten_array($input, $output=null));

Function Declaration :

函数声明:

function flatten_array($input, $output=null) {
    if($input == null) return null;
    if($output == null) $output = array();

    foreach($input as $value) {
        if(is_array($value)) {
            $output = flatten_array($value, $output);
        } else {
            array_push($output, $value);
        }
    }

    return $output;
}