Voting

: eight plus one?
(Example: nine)

The Note You're Voting On

codeslinger at compsalot dot com
14 years ago
one thing to be very aware of is that array_search() will fail if the needle is a string and the array itself contains values that are mixture of numbers and strings. (or even a string that looks like a number)

The problem is that unless you specify "strict" the match is done using == and in that case any string will match a numeric value of zero which is not what you want.

-----

also, php can lookup an index pretty darn fast. for many scenarios, it is practical to maintain multiple arrays, one in which the index of the array is the search key and the normal array that contains the data.

<?php

$normal
[$index] = array('key'=>$key, 'data'=>'foo');
$inverse[$key] = $index;

//very fast lookup, this beats any other kind of search

if (array_key_exists($key, $inverse))
{
$index = $inverse[$key];
return
$normal[$index];
}

?>

<< Back to user notes page

To Top