Jump to content

recursive search multi-dimentional array


gammaman

Recommended Posts

I am trying to search a multi-dimentional array to see if it contains one or more values within another array.

 

Here is the array I am trying to search.  It is a from a function which returns an array of the meta data found in a specified website

 

Array
(
    [title] => Video Games, Wikis, Cheats, Walkthroughs, Reviews, News & Videos - IGN
    [metaTags] => Array
        (
            [description] => Array
                (
                    [html] => <meta name="description" content="IGN is your site for Xbox 360, PS3, Wii, PC, 3DS, PS Vita & iPhone games with expert reviews, news, previews, trailers, cheat codes, wiki guides & walkthroughs" />
                    [value] => IGN is your site for Xbox 360, PS3, Wii, PC, 3DS, PS Vita & iPhone games with expert reviews, news, previews, trailers, cheat codes, wiki guides & walkthroughs
                )

            [robots] => Array
                (
                    [html] => <meta name="robots" content="noodp, noydir" />
                    [value] => noodp, noydir
                )

            [copyright] => Array
                (
                    [html] => <meta name="copyright" content="IGN Entertainment, Inc." />
                    [value] => IGN Entertainment, Inc.
                )

        )

)

 

Here is the function I am trying to use to do the search

 

function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
        $current_key=$key;
        if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {


            return true;
        }
    }
    return false;
}

 

Finally, here is the call to get the array of meta data, the array of search items to look for and the function call to the recursive_array_search

 

$link = getBaseURL();
      
$metadata = getUrlData($link);   // this function returns the array of all the meta-data


$needle = array("PC","abc");

if(recursive_array_search($needle,$metadata) == true){
	echo "VIOLATION";
}

 

 

I want this to return true b/c at least one of the items in the search items array is found within the array of meta-data.

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.