Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';在PHP中同步2个数据结构的最佳方法是什么?_Php_Mysql_Arrays_Youtube - Fatal编程技术网

什么';在PHP中同步2个数据结构的最佳方法是什么?

什么';在PHP中同步2个数据结构的最佳方法是什么?,php,mysql,arrays,youtube,Php,Mysql,Arrays,Youtube,我正在尝试将从Youtube上的特定用户检索到的一组视频同步到视频ID数据库表中 这是因为YouTube不允许向视频添加元信息。因此,我在服务器上创建了一个视频表,并希望同步VideoID i、 e.php/mysql应用程序youtube youtube视频的数据结构如下: foreach ($feed as $entry) { print "<p>"; print $entry->getVideoId(); print "</p>"; } fore

我正在尝试将从Youtube上的特定用户检索到的一组视频同步到视频ID数据库表中

这是因为YouTube不允许向视频添加元信息。因此,我在服务器上创建了一个视频表,并希望同步VideoID

i、 e.php/mysql应用程序youtube

youtube视频的数据结构如下:

foreach ($feed as $entry) {
  print "<p>";
  print $entry->getVideoId();
  print "</p>";
}
foreach($feed作为$entry){
打印“”;
打印$entry->getVideoId();
打印“

”; }
对于我的db,这是:

$rs->MoveFirst();
while (!$rs->EOF) {
  print "<p>";
  print $rs->fields['yt_id'];
  print "</p>";
  $rs->MoveNext();
}
$rs->MoveFirst();
而(!$rs->EOF){
打印“”;
打印$rs->字段['yt_id'];
打印“

”; $rs->MoveNext(); }
您知道如何同步这些数据以便:

  • 每当用户在youtube上上传新视频时,我都可以调用一个同步函数来检索最新的视频并将其添加到mysql数据库中
  • 但是,如果用户删除youtube上的视频,则不存在删除
  • 从两个位置获取ID后,您可以使用进行比较,例如:

    //build array of video IDs in YouTube
    $arYT = array();
    foreach ($feed as $entry) {
        $arYT[] = $entry->getVideoId();
    }
    
    //build array of video IDs in local DB
    $arDB = array();
    $rs->MoveFirst();
    while (!$rs->EOF) {
      $arDB[] = $rs->fields['yt_id'];
      $rs->MoveNext();
    }
    
    //to download, we want IDs which are in YouTube but not in the local Db
    $idsToDownload = array_diff($arYT, $arDB);
    
    //to delete, we want IDs which are in the local DB but not in YouTube
    $idsToDelete = array_diff($arDB, $arYT);
    
    然后你可以这样做:

    //download new videos
    foreach ($idsToDownload as $id) {
       //get video info for video $id and put into local db    
    }
    
    //delete deleted videos
    foreach ($idsToDelete as $id) {
        //delete $id from local DB
    }