<?php
class Helpers
{
/**
* Like array_merge() but case-insensitive keys comparison.
* @param array
* @param array
* @return array
*/
public static function iMerge($one, $two)
{
$keys = array_combine(array_keys(array_change_key_case($one, CASE_LOWER)), array_keys($one));
foreach ($two as $k => $v) {
if (is_int($k)) {
$one[] = $v;
} elseif (array_key_exists($lower = strtolower($k), $keys)) {
$one[$keys[$lower]] = $v;
} else {
$one[$k] = $v;
}
}
return $one;
}
}
$one = array(0, 'a' => 'aaa', 'b' => 'bbb', 1);
$two = array(2, 'A' => 'AAA', 'C' => 'CCC', 3);
print_r(Helpers::iMerge($one, $two));
preferences:
37.29 ms | 402 KiB | 5 Q