Convert multidimensional array into single array

2018-12-31 20:43发布

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?

标签: php arrays
13条回答
听够珍惜
2楼-- · 2018-12-31 21:06

You can do it just using a loop.

    $singleArray = array();

    foreach ($multiDimensionalArray as $key => $value){
        $singleArray[$key] = $value['plan'];
    }
查看更多
泪湿衣
3楼-- · 2018-12-31 21:06

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.

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

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; 
} 
查看更多
回忆,回不去的记忆
4楼-- · 2018-12-31 21:10

Your sample array has 3 levels. Because the first level has only [0], you can hardcode your access into it and avoid an extra function/construct call.

(Code Demos)

  1. array_walk_recursive() is handy and versatile, but for this task may be overkill and certainly a bit more convoluted in terms of readability.

    array_walk_recursive($array, function($leafvalue)use(&$flat){$flat[] = $leafvalue;});
    var_export($flat);
    
  2. If this was my code, I'd be using array_column() because it is direct and speaks literally about the action being performed.

    var_export(array_column($array[0], 'plan'));
    
  3. Of course a couple of `foreach() loops will perform very efficiently because language constructs generally perform more efficiently than function calls.

    foreach ($array[0] as $plans) {
        foreach ($plans as $value) {
            $flat[] = $value;
        }
    }
    var_export($flat);
    
  4. Finally, as a funky alternative (which I can't imagine actually putting to use unless I was writing code for someone whom I didn't care for) I'll offer an array_merge_recursive() call with a splat operator (...).

    var_export(array_merge_recursive(...$array[0])['plan']);
    
查看更多
不再属于我。
5楼-- · 2018-12-31 21:12

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;

}

查看更多
千与千寻千般痛.
6楼-- · 2018-12-31 21:22

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; 
} 
查看更多
公子世无双
7楼-- · 2018-12-31 21:22

Just assign it to it's own first element:

$array = $array[0];
查看更多
登录 后发表回答