Php什么是array阵列,php – 从阵列数组转换为单个数组

我该怎么做:

array(2) {

[0] => array(1) {

["bleh"] => int(109720)

}

[1] => array(1) {

["bleh"] => int(112439)

}

}

最有效率?

array(2) {

0 => 109720,

1 => 112439

}

解决方法:

我不能按照下面评论中给出的测量结果konforces回答,但是这个测量速度比使用refs的foreach快得多:

$c=count($array);

for(

$i=0;

$i

;

$array[$i]=$array[$i]['bleh'],

$i++

);

不会说实际测量它是微不足道的,时间变化有点取决于哪一个先来,这是一个有1000万成员的数组:

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

$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

$diff = microtime(1)-$start;

$tests[$test] = $diff;

unset($arrayb);

printf("%s: %f\n", $test, $diff);

标签:php,arrays,key

来源: https://codeday.me/bug/20190713/1451473.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值