Php 从阵列阵列转换为单个阵列

Php 从阵列阵列转换为单个阵列,php,arrays,key,Php,Arrays,Key,我如何做到这一点: array(2) { [0] => array(1) { ["bleh"] => int(109720) } [1] => array(1) { ["bleh"] => int(112439) } } 这是最有效的吗 array(2) { 0 => 109720, 1 => 112439 } 如果您需要快速解决方案(良好的执行速度),则: 使用 试一试 我无法遵循下面的回答中给出的测量值,但是这一

我如何做到这一点:

array(2) {
  [0] => array(1) {
    ["bleh"] => int(109720)
  }
  [1] => array(1) {
    ["bleh"] => int(112439)
  }
}
这是最有效的吗

array(2) {
  0 => 109720,
  1 => 112439
}

如果您需要快速解决方案(良好的执行速度),则:

使用

试一试


我无法遵循下面的回答中给出的测量值,但是这一测量值比使用参考值的foreach测量值要快得多:

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

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);
ini\u集('memory\u limit',-1);//我们会消耗很多。
$arrayCount=10000000;
$test='just run';
$array=数组填充(0,$arrayCount,数组(“bleh”=>109720,);
$array=结束($array);
$array=数组填充(0,$arrayCount,数组(“bleh”=>109720,);
$array=array_映射(函数(){},$array);
$test='foreach ref';
$array=数组填充(0,$arrayCount,数组(“bleh”=>109720,);
$start=microtime(1);
foreach($array as&$v)$v=$v['bleh'];
未结算(五美元);
$diff=微时间(1)-$start;
$tests[$test]=$diff;
printf(“%s:%f\n”,$test,$diff);
$test='foreach key';
$array=数组填充(0,$arrayCount,数组(“bleh”=>109720,);
$start=microtime(1);
foreach($array as$k=>$v)$array[$k]=$v['bleh'];
$diff=微时间(1)-$start;
$tests[$test]=$diff;
printf(“%s:%f\n”,$test,$diff);
$test='foreach copy';
$array=数组填充(0,$arrayCount,数组(“bleh”=>109720,);
$start=microtime(1);
foreach($k=>$v的数组)$arrayb[]=$v['bleh'];
$diff=微时间(1)-$start;
$tests[$test]=$diff;
未结算($arrayb);
printf(“%s:%f\n”,$test,$diff);
$test='array_map lambda';
$array=数组填充(0,$arrayCount,数组(“bleh”=>109720,);
$start=microtime(1);
$array=array_映射(函数($e){return$e['bleh'];},$array);
$diff=微时间(1)-$start;
$tests[$test]=$diff;
printf(“%s:%f\n”,$test,$diff);
$test='array_map reset';
$array=数组填充(0,$arrayCount,数组(“bleh”=>109720,);
$start=microtime(1);
$array=数组映射($reset',$array);
foreach($k=>$v的数组)$arrayb[]=$v['bleh'];
$diff=微时间(1)-$start;
$tests[$test]=$diff;
printf(“%s:%f\n”,$test,$diff);
$test='for key';
$array=数组填充(0,$arrayCount,数组(“bleh”=>109720,);
$start=microtime(1);
对于($i=0,$c=count($array);$i 109720,);
$start=microtime(1);

对于($i=0,$c=count($array);$i
回答得很好!使用lambda表达式,但仅限于PHP5.3。看起来不错,但效率不高。@danielsmith:w/o lambda:@Radu,用什么基准测试?可能类似于($i=0)$i@konforce,我同意写可读代码。所有答案都可读(可以说,
foreach
示例最具可读性,因为您可以在第二眼看到它时理解它的功能,而无需文档).但就执行速度而言,这是
foreach
示例的两倍,而
reset
示例的速度是这一示例的两倍。如果您有许多值,您将看到差异。
  $result = array_map("reset", $a);
$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);
<?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 )