English 中文(简体)
采用固定阵列的数值来形成一个关联阵列,其关键和价值是固定的预设物
原标题:Use flat array s values to generate an associative array whose keys and values are given a static prefix

下面的法典有一条短线:

$result = array_combine(
    array_map(fn($elem) => "key_$elem", $array), 
    array_map(fn($elem) => "value_$elem", $array)
);

PHP Sand Box

我不喜欢(从可读性观点来看)不得不在同一阵列上两次使用阵列图,然后将中间结果结合起来的想法。

问题回答

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"], []);

PHP Sand Box

我只是界定简单的职能,如:

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));

PHP Sand Box

我认为,这一令人争论的任务相当不现实(不能说是真正的使用案例)。 法典打字的偏好将转向个人偏好。

如果我们考虑学术媒介的备选办法,那么我们不仅应当比较法典的简短性,而且应当比较时间的复杂性、直接性、尽量减少功能要求,甚至记忆。

TL;DR:

  1. 传统语言构造变形(如foreach(<><>>>/code>)将始终超过功能导体。 使用发电机进一步改善性能和记忆消耗——幅度极小。

  2. 所有其他功能处理器在处决时间上都很接近,你或许会把废物开发时间推向最快的思维。

  3. 在我测试的所有方法中,只有array_reduce(>遇到灾难性记忆问题。 经过研究,我发现,将克服在现场的记忆方面的挑战(因为你不能简单地增加记忆津贴)。


现在,我将列出从

我的测试稿:(Demo)

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

如果我需要以专业方式执行这种程序,我很可能使用@User863 snippet——它是干净的、直接的、实用的,并且是拖拉机,造成不止一次。

如果我有速度或记忆问题,我就使用发电机。





相关问题
Brute-force/DoS prevention in PHP [closed]

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 ...

please can anyone check this while loop and if condition

<?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 = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

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 ...

Text as watermarking in PHP

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?

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 ...

热门标签