note 105638 added to function.array-flip
| From: | mikkel at pirateparty dot ca | Date: | Fri, 02 Sep 2011 18:07:12 +0000 |
| Subject: | note 105638 added to function.array-flip | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182909@lists.php.net to get a copy of this message | ||
h3x: "this function can be used to remove null elements form an array"
A better way to remove null elements would be to use array_filter(). It's also worth noting
that array_flip($foo) and array_combine(array_values($foo), array_keys($foo)) are equivalent in
conception, with the exception being that array_combine() converts illegal keys to strings rather
than returning an inconsistently-flipped result and throwing a bunch of errors. This may or may not
be desired behaviour.
See the following (somewhat contrived) examples:
<?php
$foo = array(
0 => null,
1 => null,
2 => 'bar',
3 => null,
4 => true,
5 => false,
6 => 'Array',
7 => array('bar', 'baz')
);
// DIFFERENT BEHAVIOURS WHEN FLIPPING ARRAYS
var_dump(array_flip($foo));
/* output (complains about everything):
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (0 => null)
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (1 => null)
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (3 => null)
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (4 => true)
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (5 => false)
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (7 => array([...]))
array(1) {
["bar"]=>
int(2) // was 2 => 'bar'
["Array"]=>
int(6) // was 6 => 'Array'
}
*/
var_dump(array_combine(array_values($foo), array_keys($foo)));
/* output (silently mapped non-string/int values to string keys):
array(4) {
[""]=>
int(5) // was 5 => false, overwrote all null values since (str) null === ""
["bar"]=>
int(2) // was 2 => 'bar'
[1]=>
int(4) // was 4 => true
["Array"]=>
int(7) // was 7 => array([...]), overwrote 6 => "Array"
}
*/
// FILTERING OUT NULL ELEMENTS
var_dump(array_flip(array_flip($foo)));
/* output (again, complains about everything):
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (0 => null)
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (1 => null)
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (3 => null)
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (4 => true)
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (5 => false)
Warning: array_flip(): Can only flip STRING and INTEGER values! [...] (7 => array([...]))
array(1) {
[2]=>
string(3) "bar"
[6]=>
string(5) "Array"
}
*/
var_dump(array_filter($foo));
/* output (filters every value that evaluates to false, which in this example would be null and
false itself):
array(3) {
[2]=>
string(3) "bar"
[4]=>
bool(true)
[6]=>
string(5) "Array"
[7]=>
array(2) {
[0]=>
string(3) "bar"
[1]=>
string(3) "baz"
}
}
*/
var_dump(array_filter($foo, create_function('$a', 'return !is_null($a);')));
/* output (filters only null values - this would be the preferred option):
array(4) {
[2]=>
string(3) "bar"
[4]=>
bool(true)
[5]=>
bool(false)
[6]=>
string(5) "Array"
[7]=>
array(2) {
[0]=>
string(3) "bar"
[1]=>
string(3) "baz"
}
}
*/
?>
----
Server IP: 66.207.199.35
Probable Submitter: 68.67.54.146
----
Manual Page -- http://www.php.net/manual/en/function.array-flip.php
Edit -- https://master.php.net/note/edit/105638
Del: integrated -- https://master.php.net/note/delete/105638/integrated
Del: useless -- https://master.php.net/note/delete/105638/useless
Del: bad code -- https://master.php.net/note/delete/105638/bad+code
Del: spam -- https://master.php.net/note/delete/105638/spam
Del: non-english -- https://master.php.net/note/delete/105638/non-english
Del: in docs -- https://master.php.net/note/delete/105638/in+docs
Del: other reasons-- https://master.php.net/note/delete/105638
Reject -- https://master.php.net/note/reject/105638
Search -- https://master.php.net/manage/user-notes.php