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.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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