note 88260 added to function.in-array
| From: | rhill at xenu-directory dot net | Date: | Sat, 17 Jan 2009 21:05:03 +0000 |
| Subject: | note 88260 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149375@lists.php.net to get a copy of this message | ||
I found out that in_array will *not* find an associative array within a haystack of associative
arrays in strict mode if the keys were not generated in the *same order*:
<?php
$needle = array(
'fruit'=>'banana', 'vegetable'=>'carrot'
);
$haystack = array(
array('vegetable'=>'carrot', 'fruit'=>'banana'),
array('fruit'=>'apple', 'vegetable'=>'celery')
);
echo in_array($needle, $haystack, true) ? 'true' : 'false';
// Output is 'false'
echo in_array($needle, $haystack) ? 'true' : 'false';
// Output is 'true'
?>
I had wrongly assumed the order of the items in an associative array were irrelevant, regardless of
whether 'strict' is TRUE or FALSE: The order is irrelevant *only* if not in strict mode.
----
Server IP: 216.235.13.26
Probable Submitter: 74.59.204.182
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/88260
Del: integrated -- https://master.php.net/note/delete/88260/integrated
Del: useless -- https://master.php.net/note/delete/88260/useless
Del: bad code -- https://master.php.net/note/delete/88260/bad+code
Del: spam -- https://master.php.net/note/delete/88260/spam
Del: non-english -- https://master.php.net/note/delete/88260/non-english
Del: in docs -- https://master.php.net/note/delete/88260/in+docs
Del: other reasons-- https://master.php.net/note/delete/88260
Reject -- https://master.php.net/note/reject/88260
Search -- https://master.php.net/manage/user-notes.php