<?php
// Simple array merging
$array1 = [10, 50, 20, 40, 30];
$array2 = [1.0, 5.0, 2.0, 4.0, 3.0];
print_r(array_merge($array1, $array2));
// Sorting table-like arrays that has sorted by key elements
$table1 = [
'2011:london' => ['city' => 'London', 'year' => 2011, 'birthrate' => 'over9000'],
'2012:birmingham' => ['city' => 'Birmingham', 'year' => 2012, 'birthrate' => 'wat'],
'2013:london' => ['city' => 'London', 'year' => 2013, 'birthrate' => 'boo boo']
];
$table2 = [
'2011:london' => ['city' => 'London', 'year' => 2011, 'comment' => 1],
'2012:london' => ['city' => 'London', 'year' => 2012, 'comment' => 22],
'2014:london' => ['city' => 'London', 'year' => 2014, 'comment' => 333]
];
print_r(array_linear_merge($table1, $table2));
// New func for merging sorted by unique key table-like arrays!
// Complexity O(m+n), where m and n are sizes of each array.
function array_linear_merge($array1, $array2) {
$iterator1 = new ArrayIterator($arrayLeft);
$iterator2 = new ArrayIterator($arrayRight);
$result = [];
while ($iterator1->valid() && $iterator2->valid()) {
switch(sign(strcmp($iterator1->key(), $iterator2->key()))) {
case 1: $result[] = $iterator2->current();
$iterator2->next();
break;
case 0: $result[] = array_merge($iterator1->current(), $iterator2->current());
$iterator1->next();
$iterator2->next();
break;
case -1: $result[] = $iterator1->current();
$iterator1->next();
break;
}
}
if (!$iterator1->valid()) {
$iterator1 = $iterator2;
}
while ($iterator1->valid()) {
$result[] = $iterator1->current();
$iterator1->next();
}
return $result;
}
function sign($number) {
return ($n > 0) - ($n < 0);
}
- Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- Array
(
[0] => 10
[1] => 50
[2] => 20
[3] => 40
[4] => 30
[5] => 1
[6] => 5
[7] => 2
[8] => 4
[9] => 3
)
Warning: Undefined variable $arrayLeft in /in/pTClp on line 24
Fatal error: Uncaught TypeError: ArrayIterator::__construct(): Argument #1 ($array) must be of type array, null given in /in/pTClp:24
Stack trace:
#0 /in/pTClp(24): ArrayIterator->__construct(NULL)
#1 /in/pTClp(19): array_linear_merge(Array, Array)
#2 {main}
thrown in /in/pTClp on line 24
Process exited with code 255. - Output for 7.0.18 - 7.0.33, 7.1.4 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
- Array
(
[0] => 10
[1] => 50
[2] => 20
[3] => 40
[4] => 30
[5] => 1
[6] => 5
[7] => 2
[8] => 4
[9] => 3
)
Notice: Undefined variable: arrayLeft in /in/pTClp on line 24
Fatal error: Uncaught InvalidArgumentException: Passed variable is not an array or object in /in/pTClp:24
Stack trace:
#0 /in/pTClp(24): ArrayIterator->__construct(NULL)
#1 /in/pTClp(19): array_linear_merge(Array, Array)
#2 {main}
thrown in /in/pTClp on line 24
Process exited with code 255. - Output for 7.3.32 - 7.3.33
- Array
(
[0] => 10
[1] => 50
[2] => 20
[3] => 40
[4] => 30
[5] => 1
[6] => 5
[7] => 2
[8] => 4
[9] => 3
)
Fatal error: Uncaught InvalidArgumentException: Passed variable is not an array or object in /in/pTClp:24
Stack trace:
#0 /in/pTClp(24): ArrayIterator->__construct(NULL)
#1 /in/pTClp(19): array_linear_merge(Array, Array)
#2 {main}
thrown in /in/pTClp on line 24
Process exited with code 255. - Output for 7.0.0 - 7.0.17, 7.1.0 - 7.1.3
- Array
(
[0] => 10
[1] => 50
[2] => 20
[3] => 40
[4] => 30
[5] => 1
[6] => 5
[7] => 2
[8] => 4
[9] => 3
)
Notice: Undefined variable: arrayLeft in /in/pTClp on line 24
Fatal error: Uncaught InvalidArgumentException: Passed variable is not an array or object, using empty array instead in /in/pTClp:24
Stack trace:
#0 /in/pTClp(24): ArrayIterator->__construct(NULL)
#1 /in/pTClp(19): array_linear_merge(Array, Array)
#2 {main}
thrown in /in/pTClp on line 24
Process exited with code 255. - Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
- Array
(
[0] => 10
[1] => 50
[2] => 20
[3] => 40
[4] => 30
[5] => 1
[6] => 5
[7] => 2
[8] => 4
[9] => 3
)
Notice: Undefined variable: arrayLeft in /in/pTClp on line 24
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' in /in/pTClp:24
Stack trace:
#0 /in/pTClp(24): ArrayIterator->__construct(NULL)
#1 /in/pTClp(19): array_linear_merge(Array, Array)
#2 {main}
thrown in /in/pTClp on line 24
Process exited with code 255. - Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
- Parse error: syntax error, unexpected '[' in /in/pTClp on line 4
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- Parse error: parse error, unexpected '[' in /in/pTClp on line 4
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/pTClp on line 4
Process exited with code 255.
preferences:
266.2 ms | 401 KiB | 464 Q