note 105638 deleted from function.array-flip by danbrown

From: Date: Fri, 02 Sep 2011 18:09:23 +0000
Subject: note 105638 deleted from function.array-flip by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182910@lists.php.net to get a copy of this message
Note Submitter: mikkel at pirateparty dot ca ---- 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" } } */ ?>

« previous php.notes (#182910) next »