【问题标题】:How would you compare the names in a list to ones in an Array?您如何将列表中的名称与数组中的名称进行比较?
【发布时间】:2011-03-28 20:03:20
【问题描述】:

我有一个电影列表,我想与我从 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";}

任何想法都会帮助我。

谢谢

【问题讨论】:

    标签: php arrays facebook api compare


    【解决方案1】:

    数据看起来是 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";    
    }
    

    【讨论】:

    • 好吧,如果我做一个 var_dump 我得到: object(stdClass)#2 (1) { ["data"]=> array(18) { [0]=> object(stdClass)#3 (4) { ["name"]=> string(15) "The Drift Bible" ["category"]=> string(5) "Movie" ["id"]=> string(12) "227431881228" [" created_time"]=> string(24) "2011-02-27T21:41:04+0000" } [1]=> object(stdClass)#4 (4) { ["name"]=> string(7) "射手" ["类别"]=> 字符串(5) "电影" ["id"]=> 字符串(15) "109671005718938" ["created_time"]=> 字符串(24) "2011-02-16T09:18: 29+0000" }
    • 可能需要检查你的变量和右括号。
    • sry 刚刚删除了她在示例中发布的数据,应该修复
    【解决方案2】:

    另一种方法:

    <?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>
    

    【讨论】:

      【解决方案3】:

      使用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));
      

      【讨论】:

      • 我会试试这个。谢谢埃里克。
      猜你喜欢
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多