See also is_array() , explode() , implode() , preg_split() , and unset() .
A simple trick that can help you to guess what diff/intersect or sort function does by name.
[suffix] assoc - additional index check. Compares both value and index.
Example: array_diff_assoc, array_intersect_assoc.
[suffix] key - index only check. Ignores value of array, compares only indexes.
Example: array_diff_key, array_intersect_key.
[suffix] **empty** - no "key" or "assoc" word in suffix. Compares values only. Ignores indexes of array.
Example: array_diff, array_intersect.
[prefix] u - will do comparison with user defined function. Letter u can be used twice in some functions (like array_udiff_uassoc), this means that you have to use 2 functions (one for value, one for index).
Example: array_udiff_uassoc, array_uintersect_assoc.
This also works with array sort functions:
[prefix] a - associative. Will preserve keys.
Example: arsort, asort.
[prefix] k - key sort. Will sort array by keys.
Example: uksort, ksort.
[prefix] r - reverse. Will sort array in reverse order.
Example: rsort, krsort.
[prefix] u - sort by user defined function (same as for diff/intersect).
Example: usort, uasort.
Be careful with type hints in callbacks when using array-traverse functions. In some cases, this may silently cause the data type of elements to change.
<?php
declare(strict_types=1);
// Missing fatal TypeError, No side effects
$unexpected = array_filter(['123', (string) PHP_INT_MAX], fn (int $item) => true);
var_dump($unexpected);
// Missing fatal TypeError, Typecasting side effect
$unexpectedTypecasting = array_map(fn (int $item) => $item, ['123', (string) PHP_INT_MAX]);
var_dump($unexpectedTypecasting);
// Missing fatal TypeError, Typecasting side effect
$unexpectedTypecasting = array_map(fn (string $item) => $item, [123, PHP_INT_MAX]);
var_dump($unexpectedTypecasting);
// Missing fatal TypeError, Typecasting side effect
$unexpectedTypecasting = array_reduce(['123', (string) PHP_INT_MAX], fn (?int $carry, int $item) => $item);
var_dump($unexpectedTypecasting);
$bigIntValue = bcadd((string) PHP_INT_MAX, '1');
// Fatal TypeError
$expectedTypeError = array_map(fn (int $item) => $item, [$bigIntValue]);
var_dump($expectedTypeError);
?>
The above example will output (PHP version 8.3.6, error_reporting E_ALL):
<?php
array(2) {
[0]=>
string(3) "123"
[1]=>
string(19) "9223372036854775807"
}
array(2) {
[0]=>
int(123)
[1]=>
int(9223372036854775807)
}
array(2) {
[0]=>
string(3) "123"
[1]=>
string(19) "9223372036854775807"
}
int(9223372036854775807)
Fatal error: Uncaught TypeError: {closure}(): Argument #1 ($item) must be of type int, string given
?>
Big arrays use a lot of memory possibly resulting in memory limit errors. You can reduce memory usage on your script by destroying them as soon as you ́re done with them. I was able to get over a few megabytes of memory by simply destroying some variables I didn ́t use anymore.
You can view the memory usage/gain by using the funcion memory_get_usage(). Hope this helps!
I need to take an element from the Array and change its position within the Array by moving the rest of the elements as required.
This is the function that does it. The first parameter is the working Array. The second is the position of the element to move and the third is the position where to move the element.
The function returns the modified Array.
<?php
function array_move_elem($array, $from, $to) {
if ($from == $to) { return $array; }
$c = count($array);
if (($c > $from) and ($c > $to)) {
if ($from < $to) {
$f = $array[$from];
for ($i = $from; $i < $to; $i++) {
$array[$i] = $array[$i+1];
}
$array[$to] = $f;
} else {
$f = $array[$from];
for ($i = $from; $i > $to; $i--) {
$array[$i] = $array[$i-1];
}
$array[$to] = $f;
}
}
return $array;
}
?>
Examples:
<?php
$array = array('Cero','Uno','Dos','Tres','Cuatro','Cinco','Seis','Siete','Ocho','Nueve','Diez');
$array = array_move_elem($array, 3, 5); // Move element in position 3 to position 5...
print_r($array);
$array = array_move_elem($array, 5, 3); // Move element in position 5 to position 3, leaving array as it was... ;)
print_r($array);
?>
Return:
<?php
Array ( [0] => Cero [1] => Uno [2] => Dos [3] => Cuatro [4] => Cinco [5] => Tres [6] => Seis [7] => Siete [8] => Ocho [9] => Nueve [10] => Diez )
Array ( [0] => Cero [1] => Uno [2] => Dos [3] => Tres [4] => Cuatro [5] => Cinco [6] => Seis [7] => Siete [8] => Ocho [9] => Nueve [10] => Diez )
?>