|
-
July 14th, 2012, 02:00 PM
#1
Search array for value.
I have this function I am using which returns all the meta-data from a given website. I am trying to come up with something so that I can search the array that is returned to see if it contains a value.
Here is an example of what the function returns. Please also see my code comments
Code:
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.
)
)
)
I am trying to use something like the following to search the array
Code:
<?php
function recursive_array_search($needle,$haystack) {
$count = 0;
foreach($haystack as $key=>$value) {
$current_key=$key;
//if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
if(in_array($value)){
$count++;
echo "VIOLATION";
// return $count;
}
}
//return false;
return $count;
}
?>
Code:
$link = getBaseURL();
$metadata = getUrlData($link) //stores the meta-data from the website
$needle = array("PC","asadf"); //set of values to search for
$violation = recursive_array_search($needle,$metadata); // I want this to return a count of 1 since the value PC is found in the meta-data
The problem I am having is that taking the following piece of the meta-data 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
)
the value is one long string. I want to search this for single words. How can I achieve this?
-
July 15th, 2012, 09:51 PM
#2
Re: Search array for value.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|