2011-03-28 92 views
1

我有一個電影列表,我想比較我從Facebook Graph API獲得的電影陣列。如何將列表中的名稱與數組中的名稱進行比較?

這裏是API越來越的例子:

{"data": [ 
    { 
    "name": "The Drift Bible", 
    "category": "Movie", 
    "id": "227431881228", 
    "created_time": "2011-02-27T21:41:04+0000" 
    }, 
    { 
    "name": "Shooter", 
    "category": "Movie", 
    "id": "109671005718938", 
    "created_time": "2011-02-16T09:18:29+0000" 
    }... 

我需要比較列表是相當大的,但這裏有一些:

Wall Street, Shooter, Young Guns, Due Date... 

基本上只是在比較「名稱」 '數據'與'我的名單'的電影標題。但想不通的語法

喜歡的東西:

if ($movies->data->name == 'movie titles list') {echo "You like the same movies";} 

我發現這一點:

if (in_array('movie titles list', $a)) {echo "You like the same movies";} 

任何想法會幫助我。

感謝

回答

2

的數據看起來是JSON編碼...

嘗試是這樣的:(json_decode

$large_data = '{"data": [ 
    { 
    "name": "The Drift Bible", 
    "category": "Movie", 
    "id": "227431881228", 
    "created_time": "2011-02-27T21:41:04+0000" 
    }, 
    { 
    "name": "Shooter", 
    "category": "Movie", 
    "id": "109671005718938", 
    "created_time": "2011-02-16T09:18:29+0000" 
    }]}'; 

$json_to_array = json_decode($large_data, true); 
var_dump(json_decode($large_data, true));  

// You should now be able to compare the two array 
echo print_r($json_to_array,true); 

編輯:

改進什麼@Eric發佈

$large_data = '{"data": [ 
    { 
    "name": "The Drift Bible", 
    "category": "Movie", 
    "id": "227431881228", 
    "created_time": "2011-02-27T21:41:04+0000" 
    }, 
    { 
    "name": "Shooter", 
    "category": "Movie", 
    "id": "109671005718938", 
    "created_time": "2011-02-16T09:18:29+0000" 
    }]}'; 

$movie_list = json_decode($large_data, true); 
$movieNames = array(); 
foreach($movie_list as $movies) { 
    foreach($movies as $movie) { 
     $movieNames[] = $movie['name']; 
    } 
} 

$myMovies = array('Wall Street', 'Shooter', 'Young Guns', 'Due Date'); 

$common_movies = array_intersect($movieNames, $myMovies); 

foreach($common_movies as $common_movie) { 
    echo "We like the same movie ".$common_movie."<br />\n";  
} 
+0

以及如果我做的var_dump我得到:對象(stdClass的)#2(1){ [「data」] => array(18){[0] => object(stdClass)#3(4){[「name」] => string(15)「The Drift Bible」[「category」] =>字符串(5)「電影」[「id」] =>字符串(12)「227431881228」 [「created_time」] => string(24)「2011-02-27T21:41:04 + 0000」} [1] => object(stdClass)#4(4){[「name」] => string(7 )「射手」[「category」] => string(5)「Movie」[「id」] => string(15)「109671005718938」[「created_time」] => string(24)「2011-02-16T09: 18:29 + 0000「} – TheRealDK 2011-03-28 20:14:10

+0

可能想檢查您的變量並關閉大括號。 – drudge 2011-03-28 20:15:57

+0

sry剛剛關閉她在示例中發佈的數據,應該修復 – 2011-03-28 20:16:50

0

使用array_intersect(array1, array2, ... arrayN)

$large_data = '{"data": [ 
    { 
     "name": "The Drift Bible", 
     "category": "Movie", 
     "id": "227431881228", 
     "created_time": "2011-02-27T21:41:04+0000" 
    }, 
    { 
     "name": "Shooter", 
     "category": "Movie", 
     "id": "109671005718938", 
     "created_time": "2011-02-16T09:18:29+0000" 
    } 
]}'; 

$movies = json_decode($large_data, true)['data']; 
$movieNames = array(); 

//Get only the name from the movie list 
foreach($movies as $movie) { 
    $movieNames[] = $movie['name']; 
} 

//Intersect with internal list 
print_r(array_intersect($movieNames, $myMovies)); 
+0

我會試試這個。謝謝埃裏克。 – TheRealDK 2011-03-28 20:30:51

0

另一種方法:

<?php 
    $json_data = '{"data": [ 
     { 
      "name": "The Drift Bible", 
      "category": "Movie", 
      "id": "227431881228", 
      "created_time": "2011-02-27T21:41:04+0000" 
     }, 
     { 
      "name": "Shooter", 
      "category": "Movie", 
      "id": "109671005718938", 
      "created_time": "2011-02-16T09:18:29+0000" 
     } 
     ]}'; 

    $array_data = json_decode($json_data, TRUE); 

    $compare_list = array(
     'Wall Street', 
     'Shooter', 
     'Young Guns', 
     'Due Date' 
    ); 

    // <Marco Stumper> phpundhtml at web dot de 
    function in_array_multi($needle, $haystack) { 
     $found = false; 
     foreach ($haystack as $value) { 
      if ((is_array($value) && in_array_multi($needle, $value)) || $value == $needle) { 
       $found = true; 
      } 
     } 
     return $found; 
    } 

    foreach ($compare_list as $item) { 
     echo '<p>' . $item . (in_array_multi($item, $array_data) ? ' DOES ' : ' does NOT ') . 'exist within $array_data</p>' . PHP_EOL; 
    } 
?> 

輸出:

<p>Wall Street does NOT exist within $array_data</p> 
<p>Shooter DOES exist within $array_data</p> 
<p>Young Guns does NOT exist within $array_data</p> 
<p>Due Date does NOT exist within $array_data</p> 
相關問題