Array
Operations
As we mentioned in the PHP Basics blog,
a number of operators behave differently if their operands are arrays. For
example, the addition operator + can be used to create the union of its two
operands:
$a = array (1, 2, 3);
$b = array (’a’ =>
1, ’b’ => 2, ’c’ => 3);
var_dump ($a + $b);
This outputs the following:
array(6) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
Note how the resulting array
includes all of the elements of the two original arrays, even though they have
the same values; this is a result of the fact that the keys are different—if
the two arrays had common elements that also share the same string keys or that
have numeric keys (even if they are different), they would only appear once in
the end result:
$a = array (1, 2, 3);
$b = array (’a’ =>
1, 2, 3);
var_dump ($a + $b);
This results in:
array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
["a"]=>
int(1)
}
Comparing
Arrays
Array-to-array comparison is a
relatively rare occurrence, but it can be performed using another set of
operators. Like for other types, the equivalence and identity operators can be
used for this purpose:
$a = array (1, 2, 3);
$b = array (1 => 2,
2 => 3, 0 => 1);
$c = array (’a’ =>
1, ’b’ => 2, ’c’ => 3);
var_dump ($a == $b);
// True
var_dump ($a === $b);
// False
var_dump ($a == $c);
// True
var_dump ($a === $c);
// False
As you can see, the equivalence operator
== returns true if both arrays have the same number of elements with the same
values and keys, regardless of their order. The identity operator ===, on the
other hand, returns true only if the array contains the same key/value pairs in
the same order. Similarly, the inequality and non-identity operators can
determine whether two arrays are different:
$a = array (1, 2, 3);
$b = array (1 => 2,
2 => 3, 0 => 1);
var_dump ($a != $b);
// False
var_dump ($a !== $b);
// True
Once again, the inequality operator
only ensures that both arrays contain the same elements with the same keys,
whereas the non-identity operator also verifies their position.
Counting, Searching and Deleting
Elements The size of an array can be retrieved by calling the count() function:
$a = array (1, 2, 4);
$b = array();
$c = 10;
echo count ($a); //
Outputs 3
echo count ($b); //
Outputs 0
echo count ($c); //
Outputs 1
As you can see, count() cannot be
used to deter mine whether a variable contains
an array—since running it on a
scalar value will return one. The right way to tell
whether a variable contains an array
is to use is_array() instead.
A similar problem exists with
determining whether an element with the given key
exists. This is often done by
calling isset():
$a = array (’a’ =>
1, ’b’ => 2);
echo isset ($a[’a’]);
// True
echo isset ($a[’c’]);
// False
However, isset() has the major
drawback of considering an element whose value is
NULL—which is perfectly valid—as
inexistent:
$a = array (’a’ =>
NULL, ’b’ => 2);
echo isset ($a[’a’]);
// False
The correct way to determine whether
an array element exists is to use array_key_exists() instead:
$a = array (’a’ =>
NULL, ’b’ => 2);
echo array_key_exists
($a[’a’]); // True
Obviously, neither these functions
will al low you to determine whether an element
with a given value exists in an
array—this is, instead, performed by the in_array() function:
$a = array (’a’ =>
NULL, ’b’ => 2);
echo in_array ($a, 2);
// True
Finally, an element can be deleted
from an array by unsetting it:
$a = array (’a’ =>
NULL, ’b’ => 2);
unset ($a[’b’]);
echo in_array ($a, 2);
// False
Flipping
and Reversing
There are two functions that have
rather confusing names and that are sometimes misused: array_flip() and array_reverse().
The first of these two functions inverts the value of each element of an array
with its key:
$a = array (’a’, ’b’,
’c’);
var_dump (array_flip
($a));
This outputs:
array(3) {
["a"]=>
int(0)
["b"]=>
int(1)
["c"]=>
int(2)
}
On the other hand, array_reverse()
actually inverts the order o f the array’s elements,
so that the last one appears first:
$a = array (’x’ =>
’a’, 10 => ’b’, ’c’);
var_dump
(array_reverse ($a));
Note how key key association is only
lost for those elements whose keys are numeric:
array(3) {
[0]=>
string(1)
"c"
[1]=>
string(1)
"b"
["x"]=>
string(1)
"a"
}
0 Comment to " PHP Array Operators "
Post a Comment