Skip to main content

Search

Navigation

Sponsor: The CSS Anthology

Blog Entry

Useful Function: Multiple-dimension Array Search

Published on the 31st of January 2008

By Ethan Poole

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!

Tags

Comments

Sponsor: Songbird Media Player

Applications

Categories

Use Opera

Opera 9. Innovation delivered. Download Now (external link)

Archives

Author Profile

Ethan Poole

Ethan is a PHP developer from the sunny state of Florida. He is the Managing Director of Lowter, directing and overseeing all production. Ethan is a crazy fan of the Opera (external link) web browser and he enjoys foreign language.

All Articles by Ethan

Additional Navigation

Copyright © 2004-2008 Lowter

Sponsor Links