• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

不要在循环体中使用 array_merge ()

php 搞代码 4年前 (2022-01-21) 30次浏览 已收录 0个评论

标题是不要在循环体中使用 array_merge(),其实这只是本篇文章的结论之一

下面我们一起研究一下 php 语言中数组的合并(这里先不考虑递归合并)

四种合并数组的方式对比

四种常见的合并数组的方式对比

写代码

我们知道 array_merge() 和 运算符 + 都可以拼接数组

创建一个类

ArrayMerge()
● eachOne() 循环体使用 array_merge() 合并
● eachTwo() 循环体结束后使用 array_merge() 合并
● eachThree() 循环体嵌套实现数组合并
● eachFour() 循环体使用 运算符 + 拼接合并
● getNiceFileSize() 将内存占用转化成人类可读的方式

/** * Class ArrayMerge */class ArrayMerge{    /**     * @param int $times     * @return array     */    public static function eachOne(int $times): array    {        $a = [];        $b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];        for ($i = 0; $i < $times; $i++) {            $a = array_merge($a, $b);        }        return $a;    }    /**     * @param int $times     * @return array     */    public static function eachTwo(int $times): array    {        $a = [[]];        $b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];        for ($i = 0; $i < $times; $i++) {            $a[] = $b;        }        return array_merge(...$a);    }    /**     * @param int $times     * @return array     */    public static function <strong>*本文来@源gao@daima#com搞(%代@#码@网2</strong><pre>搞代gaodaima码

eachThree(int $times): array { $a = []; $b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; for ($i = 0; $i < $times; $i++) { foreach ($b as $item) { $a[] = $item; } } return $a; } /** * @param int $times * @return array */ public static function eachFour(int $times): array { $a = []; $b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; for ($i = 0; $i < $times; $i++) { $a = $b + $a; } return $a; } /** * 转化内存信息 * @param $bytes * @param bool $binaryPrefix * @return string */ public static function getNiceFileSize(int $bytes, $binaryPrefix = true): ?string { if ($binaryPrefix) { $unit = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'); if ($bytes === 0) { return '0 ' . $unit[0]; } return @round($bytes / (1024 ** ($i = floor(log($bytes, 1024)))), 2) . ' ' . ($unit[(int)$i] ?? 'B'); } $unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); if ($bytes === 0) { return '0 ' . $unit[0]; } return @round($bytes / (1000 ** ($i = floor(log($bytes, 1000)))), 2) . ' ' . ($unit[(int)$i] ?? 'B'); }}

使用

先分配多点内存

输出内存占用,合并后的数组长度,并记录每一步的用时

ini_set('memory_limit', '4000M');$timeOne = microtime(true);$a       = ArrayMerge::eachOne(10000);echo 'count eachOne Result | ' . count($a) . PHP_EOL;echo 'memory eachOne Result | ' . ArrayMerge::getNiceFileSize(memory_get_usage(true)) . PHP_EOL;$timeTwo = microtime(true);$b       = ArrayMerge::eachTwo(10000);echo 'count eachTwo Result | ' . count($b) . PHP_EOL;echo 'memory eachTwo Result | ' . ArrayMerge::getNiceFileSize(memory_get_usage(true)) . PHP_EOL;$timeThree = microtime(true);$c         = ArrayMerge::eachThree(10000);echo 'count eachThree Result | ' . count($c) . PHP_EOL;echo 'memory eachThree Result | ' . ArrayMerge::getNiceFileSize(memory_get_usage(true)) . PHP_EOL;$timeFour = microtime(true);$d        = ArrayMerge::eachFour(10000);echo 'count eachFour Result | ' . count($d) . PHP_EOL;echo 'memory eachFour Result | ' . ArrayMerge::getNiceFileSize(memory_get_usage(true)) . PHP_EOL;$timeFive = microtime(true);echo PHP_EOL;echo 'eachOne | ' . ($timeTwo - $timeOne) . PHP_EOL;echo 'eachTwo | ' . ($timeThree - $timeTwo) . PHP_EOL;echo 'eachThree | ' . ($timeFour - $timeThree) . PHP_EOL;echo 'eachFour | ' . ($timeFive - $timeFour) . PHP_EOL;echo PHP_EOL;

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:不要在循环体中使用 array_merge ()

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址