Breadcrumbs

Useful Function: Multiple-dimension Array Search

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:

Code: 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

  • 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 wink

    Posted by -=Hero Doug=- on Thu 31 Jan 2008 at 21:46

  • 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 smile

    Posted by Tom (external link) on Fri 1 Feb 2008 at 5:23

  • 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.

    Posted by Ethan Poole (external link) on Fri 1 Feb 2008 at 6:54

  • 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:

    Code: 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.

    Posted by Eugene Wee on Fri 1 Feb 2008 at 9:43

  • There might be multiple needles in the haystack. I'd go with what laserlight said. =)

    Posted by Frans (external link) on Fri 1 Feb 2008 at 11: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.

    Posted by Ethan Poole (external link) on Sat 2 Feb 2008 at 12:18

  • 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.

    Posted by Eugene Wee on Sat 2 Feb 2008 at 12:45