Reputation: 63
I Have A json Array from hxxp://best1st.info/Moviedb/json.php?m=tt2015381&o=json
Here is sample of the array output :
. and so on
.
.
[STORYLINE] => On planet Earth in 1988, young Peter Quill ( ) sits in the waiting room of a hospital...
[ALSO_KNOWN_AS] => Array
(
[0] => Guardianes de la galaxia = Argentina
[1] => Qalaktikanin Mühafizeçileri = Azerbaijan
[2] => Пазителите на Галактиката = Bulgaria (Bulgarian title)
[3] => Guardiões da Galáxia = Brazil
)
[RELEASE_DATES] => Array
(
.
.
. and so on
I want to check if in "ALSO_KNOWN_AS" Element, have "Argentina" word , If "ALSO_KNOWN_AS" Element have the "Argentina" word, then display it (the value).
I have try to do it (search with google and here on stackoverflow), but seem my code dont work, Can some one here help me to fix it , here is my code
$url = 'http://best1st.info/Moviedb/json.php?m=tt2015381&o=json';
$newdata = json_decode(file_get_contents($url));
$alsoKnownAs = $newdata->ALSO_KNOWN_AS;
if (in_array('Argentina', $alsoKnownAs)) {
echo "Match found";
// echo the-array-value
}
else
{
echo "Match not found";
return false;
}
Thanks You
Upvotes: 0
Views: 69
Reputation: 3710
Your entries in the ALSO_KNOWN_AS
array are not single words like "Argentina" but rather comparisons with country names and other text. This is why in_array()
won't find it. in_array()
needs precise matches. So you have to check all items.
I suggest you replace the if statement by a foreach loop:
...
...
$found = false;
foreach ($alsoKnownAs as $item) {
if (strpos($item, 'Argentina') !== false) {
$found = true;
break;
}
}
if (!$found) {
echo "Match not found";
return false;
}
Hope this helps :-)
Upvotes: 0
Reputation: 2506
function searchArray($search, $array)
{
foreach($array as $key => $value)
{
if (stristr($value, $search))
{
return true;
}
}
return false;
}
Here in the above function first argument is the string you like to search and second argument is the array and it will iterate through entire array with the function stristr
whose work is to return portion on found else false.
Note: if you need case sensitive search instead of stristr
use strstr
or strchr
if (searchArray('Argentina', $alsoKnownAs)) {
echo "Match found";
// echo the-array-value
}
else
{
echo "Match not found";
return false;
}
Upvotes: 0
Reputation: 9635
try this
$url = 'http://best1st.info/Moviedb/json.php?m=tt2015381&o=json';
$newdata = json_decode(file_get_contents($url));
$found = false;
foreach($newdata->ALSO_KNOWN_AS as $value)
{
if(strpos($value, "Argentina") !==false)
{
echo $value;
echo "<br/>";
$found = true;
}
}
if($found===false)
{
echo "Not found";
}
Upvotes: 1