0

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

3 Answers 3

1

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";
}
Sign up to request clarification or add additional context in comments.

Comments

0
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;
} 

Comments

0

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

2 Comments

this give me "Parse error: syntax error, unexpected 'as' (T_AS), expecting ';' in D:\XAMPP\htdocs..." massage, but the above answer have solve my problem thaks q anyway peter_the_oak :)
Yes, it must be a foreach loop :-) I've corrected the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.