2011-03-28 91 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> 
相关问题