note 55432 added to function.in-array
| From: | tacone at gmx dot net | Date: | Wed, 03 Aug 2005 14:05:56 +0000 |
| Subject: | note 55432 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93005@lists.php.net to get a copy of this message | ||
Beware of type conversion!
This snippet will unset every 0 key element form the array, when cycling an array which contains at
least one _num value.
This is because php tries to convert every element of $forbidden_elements to integer when
encountering a numeric index into array.
So $array[0] it's considered equal to (int)'_num'.
<?php
$forbidden_elements=array('_num');
foreach ($array as $key=>$value){
if (in_array($key,$forbidden_elements)){
unset ($array[$key]);
}
}
?>
The following example works, anway you can use strict comparison as well.
<?php
$forbidden_elements=array('_num');
foreach ($array as $key=>$value){
if (in_array($key,$forbidden_elements) && is_string($key)){
unset ($array[$key]);
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55432
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55432&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55432&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55432&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55432&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55432&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55432&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55432&report=yes
Search -- http://master.php.net/manage/user-notes.php