Sponsor: The CSS Anthology
The CSS Anthology: 101 Essential Tips, Tricks & Hacks (external link)
Blog Entry
Useful Function: Multiple-dimension Array Search
Published on the 31st of January 2008
Awhile back I found that I really needed a way to search through multi-dimensional arrays. PHP has the in_array() function built-in to the language, but I needed a similar function that would search through an array with more than one dimension. I use the following function in_array_multiple() to get this job done and it works perfectly. It is a very simple function, but it does exactly what it needs to:
PHP
<?php
/**
* Multiple-dimensional array search
*
* @param string $needle Value searching for
* @param array $haystack Array being searched
* @return boolean
*/
function in_array_multiple($needle, $haystack)
{
$found = FALSE;
foreach ($haystack as $value)
{
if ((is_array($value) && in_array_multiple($needle, $value)) || $value == $needle)
{
$found = TRUE;
break;
}
}
return $found;
}
?>
Now, I cannot remember if I wrote this function from scratch, reworked a function I found somewhere, or simply found this function on a website. It was too long ago when I added it to the functions.php file that I include in every project of mine. Nevertheless, it is a very useful function indeed!
Comments
-
Doug (31 Jan 2008 21:46)
Very nice, but why not return control right after the value has been found instead of letting it finish searching. It's not like you need to continue searching ;)
-
Tom (external link) (1 Feb 2008 5:23)
Nice. If you are using a multi-dimensional array though, couldn't you use some sort of hashing function (probably only apply to 2 dimensional actually) where you think of the 2 dimensionial array as a table. The first column in each row represents a value which may be produced by the hash function and all the rest of the columns provide spaces where values could be stored.
This probably can't even be implemented for what you wanted to do, just trying to move away from the old sequential search :)
-
Ethan Poole (external link) (1 Feb 2008 6:54)
Doug, you're right so I'll make that change to the function when I have a chance.
Tom, for 2-dimensional arrays (just rows and columns) there are already built-in PHP functions to get things done. This function is for arrays with nested arrays within them, and it works well without being too complex.
-
Eugene Wee (1 Feb 2008 9:43)
It seems that Doug mentioned has mentioned this*, but let's see if php code tags work for comments...
If you are willing to allow multiple points of exit, it would be more efficient to write:
PHP
<?php
function in_array_multiple($needle, $haystack)
{
foreach ($haystack as $value)
{
if ((is_array($value) && in_array_multiple($needle, $value)) || $value == $needle)
{
return true;
}
}
return false;
}
?>Otherwise, a break after setting $found to true would be appropriate to avoid unnecessary comparison.
* I really should have been less pre-occupied with the alignment of "Website:" and "Comment:", gah.
-
Frans (external link) (1 Feb 2008 11:18)
There might be multiple needles in the haystack. I'd go with what laserlight said. =)
-
Ethan Poole (external link) (2 Feb 2008 12:18)
Okay, I have followed Doug pointing out that it need not continue once the needle is found. However, unlike laserlight's code, I much prefer to keep my functions organised a particular way with the return always at the bottom. The difference is minor.
-
Eugene Wee (2 Feb 2008 12:45)
However, unlike laserlight's code, I much prefer to keep my functions organised a particular way with the return always at the bottom.
Ah, so you are one of those single point of exit advocates. In that case a break, as I mentioned earlier, would be appropriate.
Sponsor: Songbird Media Player
Desktop web player, a digital jukebox, and web browser mash-up. (external link)

