|
|
 |
array_search (PHP 4 >= 4.0.5, PHP 5) array_search --
Searches the array for a given value and returns the
corresponding key if successful
Descriptionmixed array_search ( mixed needle, array haystack [, bool strict] )
Searches haystack for
needle and returns the key if it is found in
the array, FALSE otherwise.
Note:
If needle is a string, the comparison is done
in a case-sensitive manner.
Note:
Prior to PHP 4.2.0, array_search() returns
NULL on failure instead of FALSE.
If the optional third parameter strict is set to
TRUE then the array_search()
will also check the types of the needle
in the haystack.
If needle is found in
haystack more than once, the first matching key
is returned. To return the keys for all matching values, use
array_keys() with the optional
search_value parameter instead.
Example 1. array_search() example |
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); $key = array_search('red', $array); ?>
|
|
| Warning | This function may
return Boolean FALSE, but may also return a non-Boolean value which
evaluates to FALSE, such as 0 or
"". Please read the section on Booleans for more
information. Use the ===
operator for testing the return value of this
function. |
See also array_keys(),
array_values(),
array_key_exists(), and
in_array().
User Contributed Notes
array_search
kmkz [AT] kmkz [DOT] com
07-May-2005 09:46
I always wanted an array_replace function that could search and replace within an array, so I made one (its kinda simple and a bit inefficient, but c'est la vie):
<?php
function array_replace($search, $replace, &$array) {
foreach($array as $key => $value) {
if($value == $search) {
$array[$key] = $replace;
}
}
}
?>
vlad dot gladin at 102mg dot ro
05-Apr-2005 05:33
I found this useful especially for parsing htmls from
$file=file("http://a.website.com/htmlpage.html");
function array_search_extended($file,$str_search)
{
foreach($file as $line)
{
if (strpos($line, $str_search)!== FALSE)
{
return $line;
}
}
return false;
}
hanafi at pixella dot com
21-Jan-2005 06:37
If you want to search in a multi-dimensional array and just a portion, not the whole value, try this.
<?
$Projects[0] = array(123, "Text 1");
$Projects[1] = array(456, "Text 2");
$Projects[2] = array(789, "Text 3");
$search_value = "ext 2";
foreach ($Projects as $key => $row)
{
foreach($row as $cell)
{
if (strpos($cell, $search_value) !== FALSE)
{
echo "<p>".$key;
}
}
}
?>
Swoog DOT News A@T LaPoste DOT NET_DELME
17-Jan-2005 10:48
[[Excuse me but the return line of the precedent fuction had an error :( :( :( ]]
Just For Say I Had Reworked the function Object_Search in Object_Search_All :
<?php
function object_search_all(&$needle, &$haystack, $strict=false) {
$results = array();
if(!is_array($haystack)) return NULL;
foreach($haystack as $k => $v) {
if(($strict && $needle===$v) || (!$strict && $needle==$v)) $results[] = $k;
}
return $results == array() ? false : $results;
}
?>
it's return _NULL_ if $haystack isn't an array;
it's retrun _false_ if there is no $needle in haystack
it work also with not numerics array (thanks to a _foreach_ )
Be Happy ;) ! :D
Bye !
Swoog DOT News A@T LaPoste DOT NET_DELME
17-Jan-2005 10:29
for "voituk on asg dot kiev dot ua" :
no need to do :
(get_class($needle)==get_class($haystack[$i])) && ($needle==$haystack[$i])
for type sensible searches, Because, in PHP, two objects are equals if and only if they have same class and all of their attributes are the same (values).
But Because of the untyping of PHP, same attributes can be == but not === so, to avoid problems with type sensible searches, do :
$needle===$haystack[$i]
Is Better ! ;)
stew at initiative dot uk dot com(stewart boutcher)
05-Jan-2005 09:54
Not to labour the point too much, but here's another, slightly less verbose multi dimensional recursive function...
function array_key_exists_recursive($needle,$haystack) {
foreach($haystack as $key=>$val) {
if(is_array($val)) { if(array_key_exists_recursive($needle,$val)) return 1; }
elseif($val == $needle) return 1;
}
return 0;
}
pornsak at neowin dot net
21-Nov-2004 09:28
This is a modified version of Mark Meves's wonderful function. I needed something that would be able to let me force search the key name where the needle should be found.
<?php
function array_search_recursive($needle, $haystack, $key_lookin="") {
$path = NULL;
if (!empty($key_lookin) && array_key_exists($key_lookin, $haystack) && $needle === $haystack[$key_lookin]) {
$path[] = $key_lookin;
} else {
foreach($haystack as $key => $val) {
if (is_scalar($val) && $val === $needle && empty($key_lookin)) {
$path[] = $key;
break;
}
elseif (is_array($val) && $path = array_search_recursive($needle, $val, $key_lookin)) {
array_unshift($path, $key);
break;
}
}
}
return $path;
}
?>
scripts at webfire dot org
03-Nov-2004 11:13
* Multi-Dimensional Array Search *
If you're searching for a function to search in Multi-Arrays,
this is probably usefull for you.
-------------------------------------------------------------
<?php
function multi_array_search($search_value, $the_array)
{
if (is_array($the_array))
{
foreach ($the_array as $key => $value)
{
$result = multi_array_search($search_value, $value);
if (is_array($result))
{
$return = $result;
array_unshift($return, $key);
return $return;
}
elseif ($result == true)
{
$return[] = $key;
return $return;
}
}
return false;
}
else
{
if ($search_value == $the_array)
{
return true;
}
else return false;
}
}
?>
-------------------------------------------------------------
It will return an Array with the keys from the original array
where your search-string was found or false. e.g.:
-------------------------------------------------------------
<?php
$foo[1]['a']['xx'] = 'bar 1';
$foo[1]['b']['xx'] = 'bar 2';
$foo[2]['a']['bb'] = 'bar 3';
$foo[2]['a']['yy'] = 'bar 4';
$foo['info'][1] = 'bar 5';
$result = multi_array_search('bar 3', $foo);
print_r($result);
?>
-------------------------------------------------------------
Output:
Array
(
[0] => 2
[1] => a
[2] => bb
)
-------------------------------------------------------------
I hope you like it ;)
greetz Udo
Félix Cloutier <felixcca at yahoo dot ca>
22-Oct-2004 07:01
There is no function to count the occurences of needle in haystack, so I made my own one...
<?php
function array_match($needle, $haystack)
{
if( !is_array($haystack) ) return false;
$i = 0;
while( (in_array( $needle, $haystack )) != FALSE )
{
$i++;
$haystack[array_search($needle, $haystack)] = md5($needle);
reset($haystack);
}
return $i;
}
?>
I know it's a bit crappy, but don't ask me too much, I'm still only 13... ;)
voituk on asg dot kiev dot ua
29-Sep-2004 07:04
There is no way to use this function to search an object in the array.
I want to suggest my own function for this problem solve
<?php
function object_search($needle, $haystack, $strict=false) {
if (!is_array($haystack)) return false;
for ($i=0; $i<count($haystack); ++$i) {
if ($strict) {
if ((get_class($needle)==get_class($haystack[$i])) && ($needle==$haystack[$i])) return $i;
} else {
if ($needle==$haystack[$i]) return $i;
}
}
return false;
}?>
leaetherstrip ATNOSPAM inbox DOT ru
28-Sep-2004 08:07
For get dennis dot decoene 's binary_search working on arrays with 1 or 2 elements, just replace
<?php
while ($high - $low > 1){
?>
with
<?
while ($high - $low >= 1){
?>
Wouter van Vliet <me at woutervanvliet dot nl>
19-Aug-2004 10:55
I was looking for a way to use a user defined function for array_search and eventually came up writing my own. Which was remarkably simple :P. Let me share this:
<?php
function array_usearch($cb, $ndl, $hs, $strict=false) {
if (!is_array($hs)) user_error('Third argument to array_usearch is expected to be an array, '.gettype($hs).' given', E_USER_ERROR);
foreach($hs as $key=>$value) if (call_user_func_array($cb, Array($ndl, $value, $key, $strict))) return $key;
};
?>
I'm not sure if I'm following correct conventions to specify the callback as the first argument, but it seemed most logical to me to not interrupt the order of the other four arguments (mixed needle, array haystack, boole strict).
[so far for my first post to the php notes]
andrey at php dot net
07-Aug-2004 06:56
array_search() has kind of hidden behaviour which comes from the way PHP compares values of different types (PHP is a type-loose language) - so called type juggling.
for example :
<?php
$a=array(0,0,5,0,0);
var_dump(array_search(true, $a));
?>
In the array there are only integers but we give a boolean value TRUE for be the needle. The result is that array_search() returns the first non-negative value in the haystack array. The same way if we pass FALSE it will return the first value that compared with FALSE gives TRUE - for example NULL
<?php
$a=array(1,NULL,5,0,0);
var_dump(array_search(FALSE, $a));
?>
Returns:
int(1) <-- the key of the NULL value
softexpert [at] libertysurf [dot] fr
03-May-2004 01:51
I use this for searching for a value in a bidimensional array .
<?php
function SearchBiDimArray(&$theArray, $dimNo, $searchValue, $returnIndex = true){
if(is_array($theArray)){
$keys = array_keys($theArray[0]);
$key = $keys[$dimNo];
$elcount = count($theArray);
for($i=0; $i < $elcount; $i++){
if($theArray[$i][$key] === $searchValue){
if ($returnIndex){
return $i;
}
else{
return $theArray[$i];
}
}
}
}
else{
return array_search($searchValue, $theArray);
}
}
$theArray = array();
$theArray[0]['firstproperty'] = 'avalue1';
$theArray[0]['secondproperty'] = 'anothervalue1';
$theArray[1]['firstproperty'] = 'avalue2';
$theArray[1]['secondproperty'] = 'anothervalue2';
$theArray[2]['firstproperty'] = 'avalue3';
$theArray[2]['secondproperty'] = 'anothervalue3';
print SearchBiDimArray($theArray, 1, 'anothervalue2', true);
print SearchBiDimArray($theArray, 1, 'anothervalue2', true);
?>
ivan dot schmid at astalavista dot ch
28-Apr-2004 11:46
This is a simple Array-search & delete example.
============================================
<?PHP
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
if(in_array('blue', $array))
{
unset($array[array_search('blue', $array)]); }
print_r($array);
?>
============================================
Hope this helps someone.
Greetings Ivan
manicdepressive at mindless dot com
27-Apr-2004 09:49
<?php
function array_search_recursive( $needle, $haystack )
{
$path = NULL;
$keys = array_keys($haystack);
while (!$path && (list($toss,$k)=each($keys))) {
$v = $haystack[$k];
if (is_scalar($v)) {
if ($v===$needle) {
$path = array($k);
}
} elseif (is_array($v)) {
if ($path=array_search_recursive( $needle, $v )) {
array_unshift($path,$k);
}
}
}
return $path;
}
?>
code till dawn! -mark meves
pascal at hillrippers dot ch
08-Apr-2004 01:02
@@ apolion at loliart dot com:
a thought to your example:
the $key you get from your array_search is "0" (0 => 'blue'). And as for me, 0 ist the same as FALSE, so your
<?php
if($key == false)
?>
hast to evaluate to true.
after that, with the '===' -operand, you additionally compare the type of the variable and your statement evaluates to false, because 0 !== false.
so everything seems to be okay for me. =)
apolion at loliart dot com
18-Mar-2004 04:46
This is a silly thing, but it can drive you crazy if you do not read the warning above.
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('blue', $array); echo $key;
if($key==FALSE){
echo "FALSE";
}else{
echo "TRUE";
}
?>
It will print FALSE.
Use this coding instead:
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('blue', $array); echo $key;
if($key===FALSE){
echo "FALSE";
}else{
echo "TRUE";
}
?>
It will return TRUE
bitmore.co.kr
17-Feb-2004 08:54
Look Print
<?php
function binsearch( $needle, $haystack ) {
$high = count($haystack);
$low = 0;
while ($high - $low > 1){
$probe = ($high + $low) / 2;
if ($haystack[$probe] < $needle) $low = $probe;
else $high = $probe;
print "high = $high,low = $low,probe = $probe,\$haystack[\$probe] = $haystack[$probe] <br>";
}
if ($high == count($haystack) || $haystack[$high] != $needle) return false;
else return $high;
}
$arr=array(1,3,5,7,9,10,11,13);
$searchfor = 0;
print_r( binsearch($searchfor, $arr) );
$searchfor = 1;
print_r( binsearch($searchfor, $arr) );
$searchfor = 1;
print_r( binsearch($searchfor, $arr) );
?>
dcsoboda at oakland dot edu
28-Jan-2004 03:13
I've noticed problems with array_search() when it's handling extremely large arrays.
In one example, I had a 2000 slot array with a 128 char string in each slot, and was searching for a 128 char string within the array.
It regularly returned the wrong key. I even had it print the search string, along with the found key in the array, as a test, and it would print obvious different strings.
The problem was alleviated when I ran gzcompress() on each array slot (and on my search string, obviously). In this case, no strings were longer than 67 bytes. It performed far faster and had no accuracy problems.
19-Jan-2004 06:41
In response to dennis dot decoene:
Just remember that binary search requires the array to be sorted. If it's not sorted (and you don't want to call sort() on it) then a binary search is not what you are lookng for.
dennis dot decoene at removthis dot moveit dot be
17-Jan-2004 12:41
It has been said before: array_search is VERY slow. Everyone knows binary search is fast by design. Here is an implementation.
<?php
$arr=array(1,3,5,7,9,10,11,13);
$searchfor = 6;
echo binsearch($searchfor, $arr);
function binsearch($needle, $haystack)
{
$high = count($haystack);
$low = 0;
while ($high - $low > 1){
$probe = ($high + $low) / 2;
if ($haystack[$probe] < $needle){
$low = $probe;
}else{
$high = $probe;
}
}
if ($high == count($haystack) || $haystack[$high] != $needle) {
return false;
}else {
return $high;
}
}
?>
mjwilco at yahoo dot com
26-Nov-2003 10:42
array_search() must match the whole value, not just a portion. To search for a portion, use:
<?php
$myarray = array("some apples", "some bannanas", "some oranges");
function array_search_bit($search, $array_in)
{
foreach ($array_in as $key => $value)
{
if (strpos($value, $search) !== FALSE)
return $key;
}
return FALSE;
}
$searchkey = array_search("oranges", $myarray); $searchkey = array_search_bit("oranges", $myarray); ?>
jwhite at ytztech dot com
18-Nov-2003 08:59
Searching arrays is a very slow process. I've tried to use as many strings as I can for where an array is called, if possible. Here's a quick test to show the (eye-popping) difference between searching for a match between strings and arrays:
<?php
$test_string = '';
for ($i=1; $i <= 500000; $i++) {
$test_string .= '['.$i.']';
}
$test_array = array();
for ($i=1; $i <= 5000; $i++) {
$test_array[] = $i;
}
$time_str = getmicrotime();
for ($i=1; $i <= 500000; $i++) {
strstr($i,$test_string);
}
$time_str = getmicrotime() - $time_str;
$time_array = getmicrotime();
for ($i=1; $i <= 5000; $i++) {
array_search($i,$test_array);
}
$time_array = getmicrotime() - $time_array;
echo "<H1>Test Results</H1>\r\n";
echo "<P>String Test: $time_str seconds.</P>\r\n";
echo "<P>Array Test: $time_array seconds.</P>\r\n";
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
?>
This code block takes some time to get going (because of the number of strings to place into memory), the results, even on modest hardware are staggering. Comparing 500000 strstr() operations to 5000 array_search() operations on a P3-800 with 512 MB of RAM got the following output:
[H1]Test Results[/H1]
[P]String Test: 3.09137701988 seconds.[/P]
[P]Array Test: 4.23609495163 seconds.[/P]
The getmicrotime() function came from the note on microtime(), so that's not mine...credit to "daniel141 at yahoo dot com".
STRINGS RULE!
--
Justin White
YTZ Technical Services, LLC
php at celerondude dot com
12-Nov-2003 05:07
I think array_search uses serial search because they binary search function i wrote here seems to do a better job for records that are not always at the beginning of the array.
Here it is
<?php
function binarySearch ( $a, $t, $l, $r )
{
if($t<$a[$l]||$t>$a[$r])return NULL;
while ( $l < $r )
{
$m=intval($l+$r)/2;
if($a[$m]==$t)return $m;
elseif($t<$a[$m])$r=$m-1;
elseif($t>$a[$m])$l = $m + 1;
}
if($t==$a[$r])
return $r;
return NULL;
}
?>
usage:
binarySearch ( array, target, left range, right range );
if your array is a multidimensional array, simply change the comparison method. :)
jcatchpoole at netbeans dot org
31-Jul-2003 12:55
The warning above about the return value being NULL for versions prior to PHP 4.2.0 actually goes for 4.2.1 also - ie 4.2.1 appears to be returning NULL rather than FALSE on failure for me.
cue at openxbox dot com
09-Jun-2003 09:50
If you are using the result of array_search in a condition statement, make sure you use the === operator instead of == to test whether or not it found a match. Otherwise, searching through an array with numeric indicies will result in index 0 always getting evaluated as false/null. This nuance cost me a lot of time and sanity, so I hope this helps someone. In case you don't know what I'm talking about, here's an example:
<?php
$code = array("a", "b", "a", "c", "a", "b", "b"); while (($key = array_search("a", $code)) != NULL)
{
unset($code[$key]);
}
while (($key = array_search("a", $code)) !== NULL)
{
unset($code[$key]);
}
?>
nospam at nowhere dot net
21-Apr-2003 09:40
array_search won't accept objects as needle (see http://bugs.php.net/bug.php?id=20681) - a possible workaround is sth. like that:
<?php
function _array_search ($needle, $haystick) {
foreach($haystick as $key => $val) {
if ($needle === $val) {
return($key);
}
}
return(false);
?>
}
If you require 'bool strict' add e.g. ($strict) ? $needle === $val : $needle == $val
richard at richard-sumilang dot com
16-Apr-2003 05:59
<?php
function array_search_r($needle, $haystack){
foreach($haystack as $value){
if(is_array($value))
$match=array_search_r($needle, $value);
if($value==$needle)
$match=1;
if($match)
return 1;
}
return 0;
}
?>
Darkvie
16-Mar-2003 06:53
I wanted to search a multidimensional array for a value & assign another element of array CONTAINING the searched value.
In the array I am using ($Projects), each item in $Projects contains a "Text_ID" & "Text_Value" field from a database result. I want to search for a "Text_ID" value (456 in this example) & get it's "Text_Value" value assigned to a variable called $Text_Value.
Here's how I did it:
============================================
<?PHP
$Projects[0] = array(123, "Text 1");
$Projects[1] = array(456, "Text 2");
$Projects[2] = array(789, "Text 3");
foreach ($Projects as $key => $ArrayRow)
{
if ($ArrayRow[0] == "456") {$Text_Value= $ArrayRow[1]; break; }
}
?>
============================================
Hope this helps someone.
-Darkive
retestro_REMOVE at SPAM_esperanto dot org dot il
28-Feb-2003 10:46
If you're interested in finding a line in a file, after you read it into an array using file(), you can not use array_search since the match should be exact, and lines have line-endings ('\n', '\r' or '\n\r') - or else you know for sure what your lines contain physically.
The solution is to traverse the whole array, trim() each entry and then use array_search() - or - use something like the following small function I wrote for myself:
<?php
function search_array($needle, $haystack)
{
if (!is_array($haystack) || !is_string($needle))
return false; @reset($haystack);
while (list ($key, $value) = each($haystack)) {
$value = trim($value); if ($value === $needle)
return $key; }
return false; }
?>
-----------------------------
Notes:
1. you should check the return value with === since 0 as a key equals to 'false'.
i.e. if (search_array('my_line', $my_array) === false) { ... }
2. I have no need in whitespace at the beginning of the line, therefore I use trim(). If it's important to you, use rtrim() or chop() instead.
-
- Sergey.
Fspillner at web dot de
13-Feb-2003 10:13
If you wanna find a value in multidimensional array with array_search. You should do the following:
Use For-Each, that it solves your Problem!
Example:
$map = array( array( Name=>'Jordan' ),
array( Name=>'Oneal' )
);
<?php
$search_value = "Jordan";
foreach ($map as $key => $row)
{
foreach($row as $cell)
{
if ($cell == $search_value)
print $key;
}
}
?>
ryan at wonko dot com
12-Oct-2002 04:27
If you'd rather search for values in an array that match a regular expression, this function will do the trick (note that it returns the first index matched, not the last key matched as array_search does):
<?php
function array_ereg($pattern, $haystack)
{
for($i = 0; $i < count($haystack); $i++)
{
if (ereg($pattern, $haystack[$i]))
return $i
}
return false;
}
?>
csaba at alum.mit.edu
05-Aug-2002 02:52
The following function I created should have 15 lines of code from the first if to the last return (lines 1-2,6,8-12 start with if).
Csaba Gabor
<?php
function InsertPt ($ar, $val, $searchType=0, $startPos=0, $endPos=null) {
if (!$ar || $startPos<-($aSize=sizeof($ar)) || $startPos>$aSize-1) return 0;
if ($endPos===null) $endPos = 0 - ($startPos>=0); $startPos = mod(min($aSize-1, (max (-$aSize, $startPos))),$aSize); $endPos = mod(min($aSize-1, (max (-$aSize, $endPos))), $aSize); $diff = $endPos-$startPos;
if (!($dir = ($ar[0]==$ar[$aSize-1] ? 0 : ($ar[$aSize-1]<$ar[0] ? 1 : -1)))) return (($val<$ar[0] || ($val==$ar[0] && !$searchType)) ? $startPos + ($diff<0) : $endPos + !($diff<0));
if ($diff<0) { $startPos+=$endPos; $endPos=$startPos-$endPos; $startPos-=$endPos; } if ($val<$ar[($dir<0 ? $startPos : $endPos)]) return ($dir<0 ? $startPos : $endPos+1); if ($val>$ar[($dir<0 ? $endPos : $startPos)]) return ($dir<0 ? $endPos+1 : $startPos); if ($val==$ar[($tmp=(($dir<0)==!$searchType)) ? $startPos : $endPos]) return ($tmp ? $startPos : $endPos+1); if (abs($diff)==1) return $endPos; $mid = floor(($endPos+$startPos)/2); $tmp = ($dir<0)==(($val==$ar[$mid]) ? !$searchType : ($val<$ar[$mid])); return InsertPt ($ar, $val, $searchType, $tmp ? $startPos : $mid, $tmp ? $mid : $endPos);
}
function mod($num, $base) {
return ($num - $base*floor($num/$base));
}
?>
saltymeat at hotmail dot com
24-Jun-2002 04:03
Here's how you can use array_search() to replace all occurances of a value in an array:
<?php
function array_replace($a, $tofind, $toreplace)
{
$i = array_search($tofind, $a);
if ($i === false)
{
return $a;
}
else
{
$a[$i] = $toreplace;
return array_replace($a, $tofind, $toreplace);
}
}
?>
Usage:
$a = array(1,2,3);
$a = array_replace($a, 1, 4);
echo $a[0]; // Outputs 4
dakota at dir dot bg
29-May-2002 06:44
For versions later than 4.2.0, the check isset($key) won't work properly because the function now returns false, which "isset". This change is missing in the Change Log!
So the right way to use the function is:
<?php
$key = array_search($needle, $array);
if ($key!==null&&$key!==false) {
...
}
?>
This example will work in both older and newer to 4.2.0 versions.
P.S.: My previous post where isset() is used won't work in newer versions.
phil at limbrey dot net
22-May-2002 12:52
The function posted by 'chen.avinadav@vbulletin.com' on '13-Apr-2002 04:15' is wrong!
You are matching the value not the key. It should read:
<?php
function array_search($needle, $haystack) {
$match = false;
foreach ($haystack as $key => $value) {
if ($value == $needle) {
$match = $key;
}
}
return $match;
}
?>
swbrown at ucsd dot edu
02-May-2002 03:18
Be absolutely sure to check that your code that uses array_search now checks for 'false' too if you upgrade to PHP 4.2.0!
I was using array_search in my page authentication routines and this change had the fun side-effect of causing my code to always think a user had full permissions! It was letting anyone click through to our installation of phpMyAdmin. Not good indeed!
moz at the-allens dot net
17-Apr-2002 09:29
To future proof against the 4.2.0 return change something like this should work:
<?php
$key = array_search($needle, $haystack);
if(is_null($key))
$retval = false;
?>
and then test using:
<?php
if($key !== false)
{
}
?>
admin_espace at algomas dot fr
28-Mar-2002 02:52
array search returns _a_ key not the first key. From my tests (linux-apache-php 4.1.2) it returns the last (though this may not be reliable).
i.e. the following all output 7
print_r(array_search(1,array(0,1,1,1,1,1,6,1,8,9),true));
print_r(array_search(1,array(0,1,2,3,4,5,6,1,8,9),true));
print_r(array_search(1,array(1,0,2,3,4,5,6,1,8,9),true));
radley25 at earthlink dot net
10-Feb-2002 12:40
Note to the previous post:
<?php
$map = array( 'version' => 4
, 'OS' => 'Linux'
, 'lang' => 'english'
, 'short_tags' => true
);
$key = array_search("4",$map);
print $key;
$array = array (1,13, 20);
$key = array_search("13",$array);
print $key;
?>
If you put the number in quotes it searches for the array number. Try it without the quotes.
saconner at iastate dot edu
04-Feb-2002 03:16
In PHP versions before 4.2.0 needle was not allowed to be an array. (funnily enough, at time of posting this note, we're still at ver 4.1.1 )
| |