php多维数组到单维数组

时间:2015-02-16 10:44:50

标签: php arrays

我有一个数组

array(5) {
  [0]=>
  array(1) {
    ["id"]=>
    string(1) "5"
  }
  [1]=>
  array(1) {
    ["id"]=>
    string(1) "6"
  }
  [2]=>
  array(1) {
    ["id"]=>
    string(1) "7"
  }
  [3]=>
  array(1) {
    ["id"]=>
    string(1) "8"
  }
  [4]=>
  array(1) {
    ["id"]=>
    string(1) "9"
  }
}

我想把我的数组做成:

$registrationIDs = array( "5","6","7","8","9");

我正在尝试此代码但无法正常工作

$results = array();

foreach($result as $inner) {
    $results[key($inner)] = current($inner);
}

如何有效地转换像这样的数组

5 个答案:

答案 0 :(得分:5)

尝试使用array_map。

$results = array_map (function ($e) { return $e['id']; }, $inner);

http://php.net/manual/en/function.array-map.php

顺便说一下,如果你仍然想按自己的方式去做,请试试foreach的这种形式:

$results = array ();
foreach ($inner as $key => $value)
    $results[$key] = $value['id'];

答案 1 :(得分:2)

$array = [['id'=>1],['id'=>2],['id'=>3],['id'=>4],['id'=>5],];
$result = call_user_func_array('array_merge_recursive', $array);
var_dump($result['id']);
//array(5) {
//  [0] =>
//  int(1)
//  [1] =>
//  int(2)
//  [2] =>
//  int(3)
//  [3] =>
//  int(4)
//  [4] =>
//  int(5)
//}

答案 2 :(得分:2)

好的,我看到了很多答案,所以我想知道更好的答案是什么。

<?php                                                                    

$data = array ();                                                        
for ($i = 0; $i < 1000000; $i++)                                         
  $data[$i] = array ('id' => rand ());                                   

$time0 = microtime (true);                                               

// Niols (1)                                                             
$results = array_map (function ($e) { return $e['id']; }, $data);        
$time1 = microtime (true);                                               

// Niols (2)                                                             
$results = array ();                                                     
foreach ($data as $key => $value)                                        
  $results[$key] = $value['id'];                                       
$time2 = microtime (true);                                               

// User (1)                                                              
$it =  new RecursiveIteratorIterator(new RecursiveArrayIterator($data)); 
$results = iterator_to_array($it, false);                                
$time3 = microtime (true);                                               

// User (2)                                                              
$results = array();                                                      
foreach ($data as $datum)                                                
  $results = array_merge($results, $datum);                              
$time4 = microtime (true);                                               

// sectus                                                                
$results = call_user_func_array('array_merge_recursive', $data);         
$time5 = microtime (true);                                               

//  Pankaj katiyar and Ghost                                             
$results = array_column($data, 'id');                                    
$time6 = microtime (true);                                               

var_dump ($time1-$time0);                                                
var_dump ($time2-$time1);                                                
var_dump ($time3-$time2);                                                
var_dump ($time4-$time3);                                                
var_dump ($time5-$time4);                                                
var_dump ($time6-$time5);                                                

在我的电脑上输出:

float(0.62708687782288)
float(0.35285401344299)
float(1.5429890155792) 
float(0.7408618927002) 
float(0.70525908470154)
float(0.15015292167664)

结论:

array_column超高效(但PHP 5.5+)。写一个简单的foreach似乎也非常有效。

答案 3 :(得分:0)

您可以尝试使用以下代码。 我认为这很好。

  

流程1: -

$it =  new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$l = iterator_to_array($it, false);
var_dump($l); // one Dimensional 
  

过程2: -

尝试:

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

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

答案 4 :(得分:0)

试试这个:

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

$results = array();

foreach ($array as $value)
{
    foreach($value as $innerValue)
    {
        $results[] = $innervalue;
    }
}