gpt4 book ai didi

PHP array_merge() 仅优先使用第一个数组和唯一值?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:46:46 25 4
gpt4 key购买 nike

我想将多个数组合并在一起,同时优先使用第一个数组中的值并且仅具有唯一值。有没有比使用 array_merge()array_unique()+ 运算符更快的方法?

function foo(...$params) {
$a = [
'col1',
'col2_alias' => 'col2',
'col3'
];
$merged = array_merge($a, ...$params);
$unique = array_unique($merged);
print_r($merged);
print_r($unique);
print_r($a + $unique);
}

foo(
['col4', 'col5_alias' => 'col5', 'col6'],
['col7', 'col1', 'col5_alias' => 'col5', 'col2_alias' => 'col10']);

只是合并数组给我重复的值,并覆盖第一个数组中的值:

Array
(
[0] => col1 // duplicate
[col2_alias] => col10 // overwritten
[1] => col3
[2] => col4
[col5_alias] => col5
[3] => col6
[4] => col7
[5] => col1 // duplicate
)

使用 array_unique() 显然可以修复重复值,但不能修复覆盖值:

Array
(
[0] => col1
[col2_alias] => col10
[1] => col3
[2] => col4
[col5_alias] => col5
[3] => col6
[4] => col7
)

使用+运算符后,数组就是我想要的样子

Array
(
[0] => col1
[col2_alias] => col2
[1] => col3
[2] => col4
[col5_alias] => col5
[3] => col6
[4] => col7
)

最佳答案

您认为使用 array_mergearray_unique 函数和 + 运算符会很慢是对的。我写了一些代码来衡量每个组合的速度...

这是代码...

<?php

class ArraySpeeds
{
public $la = ['col1', 'col2_alias' => 'col2', 'col3'];
public $a = ['col4', 'col5_alias' => 'col5', 'col6'];
public $b = ['col7', 'col1', 'col5_alias' => 'col5', 'col2_alias' => 'col10'];
public $c = [];

public function executionTime ($callback)
{
$start = microtime (true);

for ($i = 0; $i < 1000000; $i++) {
$callback ();
}

return round ((microtime (true) - $start) * 1000) . '/ms' . PHP_EOL;
}

public function getTimes ()
{
$array_merge_time = $this->executionTime (function () {
$this->c[0] = array_merge ($this->la, $this->a, $this->b);
});

$array_unique_time = $this->executionTime (function () {
$merged = array_merge ($this->la, $this->a, $this->b);
$this->c[1] = array_unique ($merged);
});

$addition_time = $this->executionTime (function () {
$merged = array_merge ($this->la, $this->a, $this->b);
$unique = array_unique ($merged);
$this->c[2] = $this->la + $unique;
});

$array_diff_time = $this->executionTime (function () {
$merged = array_merge ($this->a, $this->b);
$diffed = array_diff ($merged, $this->la);

$this->c[3] = array_merge ($diffed, $this->la);
});

echo print_r ($this->c[0], true), PHP_EOL;
echo print_r ($this->c[1], true), PHP_EOL;
echo print_r ($this->c[2], true), PHP_EOL;

natsort ($this->c[3]);
echo print_r ($this->c[3], true), PHP_EOL;

echo 'array_merge: ', $array_merge_time;
echo 'array_unique: ', $array_unique_time;
echo 'addition: ', $addition_time;
echo 'array_diff: ', $array_diff_time;
}
}

$arrayspeeds = new ArraySpeeds ();
$arrayspeeds->getTimes ();

这是输出...

Array
(
[0] => col1
[col2_alias] => col10
[1] => col3
[2] => col4
[col5_alias] => col5
[3] => col6
[4] => col7
[5] => col1
)

Array
(
[0] => col1
[col2_alias] => col10
[1] => col3
[2] => col4
[col5_alias] => col5
[3] => col6
[4] => col7
)

Array
(
[0] => col1
[col2_alias] => col2
[1] => col3
[2] => col4
[col5_alias] => col5
[3] => col6
[4] => col7
)

Array
(
[3] => col1
[col2_alias] => col2
[4] => col3
[0] => col4
[col5_alias] => col5
[1] => col6
[2] => col7
)

array_merge: 403/ms
array_unique: 1039/ms
addition: 1267/ms
array_diff: 993/ms

您可以看到每次添加函数调用时执行时间都会变长,其中 array_mergearray_unique 函数和 + 运算符最慢,慢了两倍多。

但是,使用 array_diff 可以在正确输出的情况下获得不错的性能,但不会正确排序。向数组添加一个 natsort 函数调用可以解决这个问题。

例如……

function foo (...$params)
{
$a = [
'col1',
'col2_alias' => 'col2',
'col3'
];

$diff = array_diff (array_merge (...$params), $a);
$merged = array_merge ($diff, $a);
natsort ($merged);
print_r ($merged);
}

关于PHP array_merge() 仅优先使用第一个数组和唯一值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37865785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com