PHP数组复制某些键,内置的功能呢?嵌套循环性能? [英] PHP array copy certain keys, built-in functions? Nested loop performance?

查看:132
本文介绍了PHP数组复制某些键,内置的功能呢?嵌套循环性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP数组,我想重复,但只能从它的键出现在另一个数组数组复制元素。

I have a PHP array that I'd like to duplicate but only copy elements from the array whose keys appear in another array.

下面是我的数组:

$data[123] = 'aaa';
$data[423] = 'bbb';
$data[543] = 'ccc';
$data[231] = 'ddd';
$data[642] = 'eee';
$data[643] = 'fff';
$data[712] = 'ggg';
$data[777] = 'hhh';

$keys_to_copy[] = '123';
$keys_to_copy[] = '231';
$keys_to_copy[] = '643';
$keys_to_copy[] = '712';
$keys_to_copy[] = '777';

$copied_data[123] = 'aaa';
$copied_data[231] = 'ddd';
$copied_data[643] = 'fff';
$copied_data[712] = 'ggg';
$copied_data[777] = 'hhh';

我可以只通过这样的数据数组循环:

I could just loop through the data array like this:

foreach ($data as $key => $value) {
  if ( in_array($key, $keys_to_copy)) {
    $copied_data[$key] = $value;
  }
}

但是,这将一个环路,它从一个MySQL结果集检索数据内发生。因此,这将是嵌套在MySQL数据循环中循环。
我通常尽量避免嵌套循环,除非有没有使用PHP的内置阵列功能让我在寻找的结果的方式。
但我也厌倦具有MySQL数据循环中嵌套循环的,我不想让MySQL的游逛。

But this will be happening inside a loop which is retrieving data from a MySQL result set. So it would be a loop nested within a MySQL data loop. I normally try and avoid nested loops unless there's no way of using PHP's built-in array functions to get the result I'm looking for. But I'm also weary of having a nested loop within a MySQL data loop, I don't want to keep MySQL hanging around.

我可能担心嵌套循环性能不必要的,因为我将永远不会被超过几百行的数据,也许10个键这样做。

I'm probably worrying about nested loop performance unnecessarily as I'll never be doing this for more than a couple of hundred rows of data and maybe 10 keys.

不过,我想知道是否有与这样的一种方式内置PHP函数。结果
我有一个看看 array_intesect_key()但是,这并不十分做到这一点,因为我的 $ keys_to_copy 数组有我所需的键作为数组值,而不是键。

But I'd like to know if there's a way of doing this with built-in PHP functions.
I had a look at array_intesect_key() but that doesn't quite do it, because my $keys_to_copy array has my desired keys as array values rather than keys.

任何人有什么想法?

干杯,B

推荐答案

我的工作了 - 我几乎拥有了above.I想我反正发布答案的完整性。希望这可以帮助别人了!

I worked it out - I almost had it above.I thought I'd post the answer anyway for completeness. Hope this helps someone out!

array_intersect_key($的数据,array_flip($ keys_to_copy))

使用 array_flip()切换 $ keys_to_copy ,因此它可以在 array_intersect_keys使用()

Use array_flip() to switch $keys_to_copy so it can be used within array_intersect_keys()

我会运行一些测试,以上面的手动回路之间的业绩比较,这个答案。我期望内置函数是快,但他们可能是pretty相等。我知道阵列是高度优化的,所以我敢肯定,这将是关闭。

I'll run some tests to compare performance between the manual loop above, to this answer. I would expect the built-in functions to be faster but they might be pretty equal. I know arrays are heavily optimised so I'm sure it will be close.

编辑:结果
我已经运行使用PHP CLI到的foreach() code比较我与我上面的答案code问题一些基准。结果是相当惊人的。结果
这里的code我曾经的标杆,我认为这是有效的:


I have run some benchmarks using PHP CLI to compare the foreach() code in my question with the code in my answer above. The results are quite astounding.
Here's the code I used to benchmark, which I think is valid:

<?php
ini_set('max_execution_time', 0);//NOT NEEDED FOR CLI

// BUILD RANDOM DATA ARRAY
$data = array();
while ( count($data) <= 200000) {
    $data[rand(0, 500000)] = rand(0, 500000);
}
$keys_to_copy = array_rand($data, 100000);

// FOREACH
$timer_start = microtime(TRUE);
foreach ($data as $key => $value) {
    if ( in_array($key, $keys_to_copy)) {
        $copied_data[$key] = $value;
    }
}
echo 'foreach: '.(microtime(TRUE) - $timer_start)."s\r\n";

// BUILT-IN ARRAY FUNCTIONS
$timer_start = microtime(TRUE);
$copied_data = array_intersect_key($data, array_flip($keys_to_copy));
echo 'built-in: '.(microtime(TRUE) - $timer_start)."s\r\n";
?>

和结果...结果
的foreach:662.217s结果
array_intersect_key:0.099s

所以这是在数组中的元素加载更快使用PHP数组函数,而不是的foreach 。我认为这将是更快,但没有太大的!

So it's much faster over loads of array elements to use the PHP array functions rather than foreach. I thought it would be faster but not by that much!

这篇关于PHP数组复制某些键,内置的功能呢?嵌套循环性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆