从二维数组转换为单一数组

3
我该怎么做才能实现这个功能:
array(2) {
  [0] => array(1) {
    ["bleh"] => int(109720)
  }
  [1] => array(1) {
    ["bleh"] => int(112439)
  }
}

如何最有效地将其转换为这个?
array(2) {
  0 => 109720,
  1 => 112439
}
5个回答

6
使用 array_map 函数。
$a = array_map(function($e) { return $e['bleh']; }, $a);

好的回答!使用lambda表达式,但这将限制它仅适用于PHP 5.3。 - danielrsmith
1
@Radu,按照什么标准?也许像 $l = count($a); for ($i=0; $i<$l; ++$i) $a[$i] = $a[$i]['bleh']; 这样的代码更快且占用更少的内存,但是这种差异真的有意义吗?通常应该专注于选择适当的算法(每个答案本质上都是相同的),编写易于理解的代码,而不要过分担心微小的优化。 - Matthew
@konforce,我同意编写易读的代码。所有答案都是易读的(可以说foreach示例最易读,因为你一看就能理解它,不需要文档)。但就执行速度而言,这个示例比foreach示例慢两倍,而reset示例比这个还要慢两倍。如果你有很多值,你会看到差异的。 - rid
@konforce,实际上for的例子只比foreach略慢一点(无关紧要的慢)。我进行了基准测试,我的评论不仅仅基于个人经验。我也不主张过早优化,也不是仅仅因为我说它不好就说“不要这样做”,我只是提出了一个论点。我以艰苦的方式发现选择快速且易于阅读的解决方案有多么重要。如果你经常使用良好的实践方法,那么从长远来看你将会少受折磨。 - rid
@Radu,但是“for”循环(按我编写的方式)应该使用一半的内存,这通常比PHP的速度更重要......因为它在处理内存时非常糟糕。在这种情况下最有效(最快)的解决方案可能是要么a)从一开始就正确构建数组,要么b)只需使用其全部的多维荣耀。 - Matthew
显示剩余4条评论

4
尝试使用 reset 函数。
  $result = array_map("reset", $a);

3
如果您需要快速解决方案(执行速度快),则:
$a = /* the original array */;
$b = array();
foreach ($a as $value) {
    $b[] = $value['bleh'];
}

2

我无法按照konforces答案下方评论中给出的测量结果进行跟踪,但是这种方法比使用refs的foreach略微更快:

$c=count($array);
for(
  $i=0;
  $i<$c
  ;
  $array[$i]=$array[$i]['bleh'],
  $i++
);

不会说实际测量起来很琐碎,但时间会根据先后顺序有所变化,这是针对包含一千万个成员的数组进行的测试,如问题所述:

foreach ref: 4.192161
foreach key: 4.383342
foreach copy: 4.222771
array_map lambda: 12.240275
array_map reset: 16.401093
for key: 3.459406
for copy: 4.690722

脚本:

ini_set('memory_limit', -1); // wer're going to consume a lot.
$arrayCount = 10000000;

$test = 'just run';
$array = array_fill(0, $arrayCount, array("bleh" => 109720,));
$array = end($array);
$array = array_fill(0, $arrayCount, array("bleh" => 109720,));
$array = array_map(function(){}, $array);

$test = 'foreach ref';
$array = array_fill(0, $arrayCount, array("bleh" => 109720,));
$start = microtime(1);
foreach($array as &$v) $v = $v['bleh'];
unset($v);
$diff = microtime(1)-$start;
$tests[$test] = $diff;
printf("%s: %f\n", $test, $diff);

$test = 'foreach key';
$array = array_fill(0, $arrayCount, array("bleh" => 109720,));
$start = microtime(1);
foreach($array as $k => $v) $array[$k] = $v['bleh'];
$diff = microtime(1)-$start;
$tests[$test] = $diff;

printf("%s: %f\n", $test, $diff);
$test = 'foreach copy';
$array = array_fill(0, $arrayCount, array("bleh" => 109720,));
$start = microtime(1);
foreach($array as $k => $v) $arrayb[] = $v['bleh'];
$diff = microtime(1)-$start;
$tests[$test] = $diff;
unset($arrayb);
printf("%s: %f\n", $test, $diff);

$test = 'array_map lambda';
$array = array_fill(0, $arrayCount, array("bleh" => 109720,));
$start = microtime(1);
$array = array_map(function($e) { return $e['bleh']; }, $array);
$diff = microtime(1)-$start;
$tests[$test] = $diff;
printf("%s: %f\n", $test, $diff);

$test = 'array_map reset';
$array = array_fill(0, $arrayCount, array("bleh" => 109720,));
$start = microtime(1);
$array = array_map('reset', $array);
foreach($array as $k => $v) $arrayb[] = $v['bleh'];
$diff = microtime(1)-$start;
$tests[$test] = $diff;
printf("%s: %f\n", $test, $diff);

$test = 'for key';
$array = array_fill(0, $arrayCount, array("bleh" => 109720,));
$start = microtime(1);
for($i=0,$c=count($array);$i<$c;$array[$i]=$array[$i]['bleh'],++$i);
$diff = microtime(1)-$start;
$tests[$test] = $diff;
printf("%s: %f\n", $test, $diff);

$test = 'for copy';
$array = array_fill(0, $arrayCount, array("bleh" => 109720,));
$start = microtime(1);
for($i=0,$c=count($array);$i<$c;$arrayb[]=$array[$i]['bleh'],++$i);
$diff = microtime(1)-$start;
$tests[$test] = $diff;
unset($arrayb);
printf("%s: %f\n", $test, $diff);

0
<?php
$all = array(
    array( "one"    => 1),
    array( "two"    => 2),
    array( "three"  => 3)
);

function getKey($item)
{
    return key($item);
}

function getVal($item)
{
    return $item[key($item)];
}
$keys   = array_map("getKey", $all);
$values = array_map("getVal", $all);
print_r($keys);
print_r($values);

OUTPUT:

Array (
    [0] => one
    [1] => two
    [2] => three ) 
Array (
    [0] => 1
    [1] => 2
    [2] => 3 )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接