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

标签 php mysql arrays youtube

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

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

即php/mysql 应用 <-> youtube

youtube 视频的数据结构如下:

foreach ($feed as $entry) {
  print "<p>";
  print $entry->getVideoId();
  print "</p>";
}

我的数据库是这样的:

$rs->MoveFirst();
while (!$rs->EOF) {
  print "<p>";
  print $rs->fields['yt_id'];
  print "</p>";
  $rs->MoveNext();
}

您知道我如何同步这些数据以便:

  1. 每当用户在 youtube 上上传新视频时,我都可以调用一个同步函数来检索最新的视频并将其添加到 mysql 数据库中?
  2. 但是,如果用户删除了 youtube 上的视频,就没有删除吗?

最佳答案

你可以使用 array_diff()从两个位置获取 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
}

关于php - 在 PHP 中同步 2 个数据结构的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1028386/

相关文章:

php - CODEIGNITER 当 user_ID 登录时如何将 user_ID 插入到 mySQL

php - PHP 中 printf() 函数的用途是什么?

ruby - 国家代码和拨号代码 ruby

c - 如何打印带有字母的板

php - ColdFusion 中的 PHP 数组相当于什么?

PHP命名空间函数冲突

php - 我想获取表中给定推荐 ID 的总用户数

JavaScript 显示无穷大。为什么?

java - Spring Mvc Hibernate编码/多行导入sql

mysql - 删除专辑及其媒体和子专辑及其媒体的正确方法