下面的法典有一条短线:
$result = array_combine(
array_map(fn($elem) => "key_$elem", $array),
array_map(fn($elem) => "value_$elem", $array)
);
我不喜欢(从可读性观点来看)不得不在同一阵列上两次使用阵列图,然后将中间结果结合起来的想法。
下面的法典有一条短线:
$result = array_combine(
array_map(fn($elem) => "key_$elem", $array),
array_map(fn($elem) => "value_$elem", $array)
);
我不喜欢(从可读性观点来看)不得不在同一阵列上两次使用阵列图,然后将中间结果结合起来的想法。
You can use array_reduce
https://www.php.net/manual/en/Function.array-reduce.php
$result = array_reduce($array, fn ($carry, $item) => $carry + ["key_$item" => "value_$item"], []);
我只是界定简单的职能,如:
function array2map($a) {
$r = []; foreach ($a as $v) $r[ key_ .$v] = value_ .$v; return $r;
}
$result = array2map($array);
感谢这些建议。 然而,我已经找到了我喜欢的解决办法:
$result = array_merge(...array_map(fn($elem) => ["key_$elem" => "value_$elem"], $array));
我认为,这一令人争论的任务相当不现实(不能说是真正的使用案例)。 法典打字的偏好将转向个人偏好。
如果我们考虑学术媒介的备选办法,那么我们不仅应当比较法典的简短性,而且应当比较时间的复杂性、直接性、尽量减少功能要求,甚至记忆。
TL;DR:
传统语言构造变形(如foreach(<><>>>/code>)将始终超过功能导体。 使用发电机进一步改善性能和记忆消耗——幅度极小。
所有其他功能处理器在处决时间上都很接近,你或许会把废物开发时间推向最快的思维。
在我测试的所有方法中,只有array_reduce(>
遇到灾难性记忆问题。 经过研究,我发现,将克服在现场的记忆方面的挑战(因为你不能简单地增加记忆津贴)。
现在,我将列出从
发电机 普通<代码>foreach()。 固定2个阵列: 将2个阵列改为与 http://www.un.org/Depts/DGACM/index_french.htm 为处理大型有效载荷而建造的代码: 我的测试稿:(Demo) 成果: 如果我需要以专业方式执行这种程序,我很可能使用@User863 snippet——它是干净的、直接的、实用的,并且是拖拉机,造成不止一次。 如果我有速度或记忆问题,我就使用发电机。
foreach(
)function generate($array) { // 1 loop over data
foreach ($array as $v) {
yield key . $v => value . $v;
}
}
function generator($array) { // 1 parent loop over data
return iterator_to_array(generate($array));
}
function construct($array) { // 1 loop over data
$result = [];
foreach ($array as $v) {
$result[ key . $v] = value . $v;
}
return $result;
}
array_combine()
with two nested array_map(
calls:function mapCombine($array) { // 3 loops over data
return array_combine(
array_map(
fn($v) => key . $v,
$array
),
array_map(
fn($v) => value . $v,
$array
),
);
}
function mapFlatten($array) { // 3 loops over data
return array_merge(
...array_map(
fn($v) => [ key . $v => value . $v],
$array
),
);
}
array_column(
):function mapUncolumn($array) { // 2 loops over data
return array_column(
array_map(
fn($v) => [ key . $v, value . $v],
$array
),
1,
0
);
}
function walk($array) { // 1 loop over data
$result = [];
array_walk(
$array,
function($v) use (&$result) {
$result[ key . $v] = value . $v;
}
);
return $result;
}
function reduce($array) { // 1 loop over data
return array_reduce(
$array,
function($result, $v) {
$result[ key . $v] = value . $v;
return $result;
},
new ArrayObject()
);
}
function returnTime(callable $function, int $repeat = 20)
{
$tests = [];
for ($i = 0; $i < $repeat; ++$i) {
$startTime = microtime(true);
$function();
$endTime = microtime(true);
$tests[] = $endTime - $startTime;
}
// Representing the average
return 1000 * array_sum($tests) / $repeat;
}
$array = range(0, 5000);
foreach ([ generate , construct , mapCombine , mapFlatten , mapUncolumn , walk , reduce ] as $test) {
echo "Duration of $test: ", returnTime(fn() => $test($array)) . PHP_EOL;
}
Duration of generate: 0.00071525573730469
Duration of construct: 0.8031964302063
Duration of mapCombine: 1.0213017463684
Duration of mapFlatten: 1.219916343689
Duration of mapUncolumn: 1.3207793235779
Duration of walk: 1.2843608856201
Duration of reduce: 1.0416030883789
I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...
<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image. And is there a better way to create ...
如何确认来自正确来源的数字。
Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...
I wonder there is a way to post a message to a facebook business page with cURL? thanks
I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...
How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...