下面我们一同研究一下 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 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;
效果
count eachOne Result | 100000 memory eachOne Result | 9 MiB count eachTwo Result | 100000 memory eachTwo Result | 14 MiB count eachThree Result | 100000 memory eachThree Result | 18 MiB count eachFour Result | 10 #注重这里 memory eachFour Result | 18 MiB eachOne | 5.21253490448 # 轮回体中运用array_merge()最慢,而且消耗内存 eachTwo | 0.0071840286254883 # 轮回体完毕后运用array_merge()最快 eachThree | 0.037622928619385 # 轮回体嵌套比轮回体完毕后运用array_merge()慢三倍 eachFour | 0.0072360038757324 # 看似也很快,然则兼并的效果有题目
● 轮回体中运用 array_merge () 最慢,而且消耗内存
● 轮回体完毕后运用 array_merge () 最快
● 轮回体嵌套比轮回体完毕后运用 array_merge () 慢三倍
● 看似也很快,然则兼并的效果有题目
兼并数组的坑
我们注重到方才的 eachFour 的效果长度只要 10
下面探讨为何会涌现如许的效果
这里拿递归兼并一同做下对照
代码
public static function test(): void { $testA = [ '111' => 'testA1', 'abc' => 'testA1', '222' => 'testA2', ]; $testB = [ '111' => 'testB1', 'abc' => 'testB1', '222' => 'testB2', 'www' => 'testB1', ]; echo 'array_merge($testA, $testB) | ' . PHP_EOL; print_r(array_merge($testA, $testB)); echo '$testA + $testB | ' . PHP_EOL; print_r($testA + $testB); echo '$testB + $testA | ' . PHP_EOL; print_r($testB + $testA); echo 'array_merge_recursive($testA, $testB) | ' . PHP_EOL; print_r(array_merge_recursive($testA, $testB)); }
效果
+ 号拼接两个数组,后者只会补充前者没有的 key,然则会保存数字索引
array_merge() 和 array_merge_recursive() 会抹去数字索引,一切的数字索引按递次从 0 最先了
array_merge($testA, $testB) | #数字索引强迫从0最先了 字符key雷同的以后者为准 Array ( [0] => testA1 [abc] => testB1 [1] => testA2 [2] => testB1 [3] => testB2 [www] => testB1 ) $testA + $testB | #testA获得保存,testB补充了testA中没有的key,数字索引获得保存 Array ( [111] => testA1 [abc] => testA1 [222] => testA2 [www] => testB1 ) $testB + $testA | #testB获得保存,testA补充了testB中没有的key,数字索引获得保存 Array ( [111] => testB1 [abc] => testB1 [222] => testB2 [www] => testB1 )
array_merge_recursive($testA, $testB) | #数字索引从0最先一连了,但数组的递次没有被损坏,雷同的字符串 `key` 兼并为一个数组
Array ( [0] => testA1 [abc] => Array ( [0] => testA1 [1] => testB1 ) [1] => testA2 [2] => testB1 [3] => testB2 [www] => testB1 )
剖析
看到这里,你肯定异常迷惑,没想到 array_merge() 另有如许的坑
我们先来看一看官方的手册
array_merge ( array $array1 [, array $... ] ) : array
array_merge () 将一个或多个数组的单位兼并起来,一个数组中的值附加在前一个数组的背面。返回作为效果的数组。
假如输入的数组中有雷同的字符串键名,则该键名背面的值将掩盖前一个值。但是,假如数组包括数字键名,背面的值将不会掩盖本来的值,而是附加到背面。
假如只给了一个数组而且该数组是数字索引的,则键名会以一连体式格局从新索引。
只要雷同的字符串键名,后边的值才会掩盖前面的值。(然则手册中没有诠释为何数字键名的索引被重置了)
那末我们来看一下源码
PHPAPI int php_array_merge(HashTable *dest, HashTable *src) { zval *src_entry; zend_string *string_key; if ((dest->u.flags & HASH_FLAG_PACKED) && (src->u.flags & HASH_FLAG_PACKED)) { // 天然数组的兼并,HASH_FLAG_PACKED示意数组是天然数组([0,1,2]) 参考http://ju.outofmemory.cn/entry/197064 zend_hash_extend(dest, zend_hash_num_elements(dest) + zend_hash_num_elements(src), 1); ZEND_HASH_FILL_PACKED(dest) { ZEND_HASH_FOREACH_VAL(src, src_entry) { if (UNEXPECTED(Z_ISREF_P(src_entry)) && UNEXPECTED(Z_REFCOUNT_P(src_entry) == 1)) { ZVAL_UNREF(src_entry); } Z_TRY_ADDREF_P(src_entry); ZEND_HASH_FILL_ADD(src_entry); } ZEND_HASH_FOREACH_END(); } ZEND_HASH_FILL_END(); } else { //遍历猎取key和vaule ZEND_HASH_FOREACH_STR_KEY_VAL(src, string_key, src_entry) { if (UNEXPECTED(Z_ISREF_P(src_entry) && Z_REFCOUNT_P(src_entry) == 1)) { ZVAL_UNREF(src_entry); } Z_TRY_ADDREF_P(src_entry); // 参考https://github.com/pangudashu/php7-internal/blob/master/7/var.md if (string_key) { // 字符串key(zend_string) 插进去或许更新元素,会增添key的计数 zend_hash_update(dest, string_key, src_entry); } else { //插进去新元素,运用自动的索引值(破案了,索引被重置的缘由在此) zend_hash_next_index_insert_new(dest, src_entry); } } ZEND_HASH_FOREACH_END(); } return 1; }
总结
综上所述,兼并数组的差别体式格局都存在肯定的缺点,然则经由过程我们上面的探讨,我们相识到
● 轮回体中运用 array_merge() 兼并数组不可取,速率差异达百倍
● array_merge() 兼并数组要慎用,假如注重 key ,且 key 可能为数字,不能运用 array_merge() 来兼并,我们能够采纳轮回体嵌套的体式格局(注重内层轮回运用 key 举行赋值操纵)
● 假如注重 key ,且 key 可能为数字,简朴兼并数组能够运用运算符 + ,然则肯定不要在轮回体中运用,由于每次运算的的效果都是生成了一个新的数组
以上就是不要在轮回体中运用 array_merge ()的细致内容,更多请关注ki4网别的相干文章!