Re: Array Searching
| From: | Joel Boonstra | Date: | Fri, 15 Nov 2002 21:26:22 +0000 |
| Subject: | Re: Array Searching | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-124658@lists.php.net to get a copy of this message | ||
> i have an array like:
>
> Array
> (
> [1] => Array
> (
> [alias] => foo
> ...
> )
> [2] => Array
> (
> [alias] => bar
> ...
> )
> ...
> )
>
> I need some way to find out, if an alias "foo" is in the array. Any idea how
> to do this?
Assuming your outer array is called $array_of_arrays:
<?php
$found = 0;
foreach ($array_of_arrays as $array) {
if ($array['alias'] == 'foo') {
$found = 1;
break;
}
}
if ($found) {
// you found it!
}
else {
// too bad!
}
?>
Or, you can look at this:
http://www.php.net/manual/en/function.in-array.php
if you're not sure what key 'foo' will be at, or this:
http://www.php.net/manual/en/function.array-search.php
if you're not sure what key 'foo' will be at and you want to know what
that key is.
Joel
--
[ joel boonstra | jboonstra@gospelcom.net ]