json跳过数组只插入需要的内容

gdrx4gfi  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(368)

我有一个问题,你可以在json数据中看到,我有3个数组,其中2个 "kind": "youtube#video", 还有一个 "kind": "youtube#playlist" ,php代码从json中插入数据,工作正常,但在json中 "kind": "youtube#playlist", 他目前不工作,我只需要从视频插入请帮助我尝试修复它一整天。如何使用播放列表跳过数组?只从视频数组中选取信息跳过其他数组,只从“kind”数组中获取信息:“youtube视频”,数组。
以json格式显示播放列表时出错
注意:第62行的d:\xampp\htdocs\test\index.php中未定义索引:videoid
62行:$videos[$key]=$items['id']['videoid'];
代码:

$loop = mysqli_query($conn, "SELECT channelid FROM users ORDER BY id")
    or die (mysqli_error($conn));

    while ($row = mysqli_fetch_array($loop))
    {
        $channelid = $row['channelid'];
                    $content = file_get_contents($url); 
        $json = json_decode($content, true);    

        if(!isset($json['items'])) continue; //skip if no items

        $videos = ['videoId'=>'','videoId1'=>'','videoId2'=>'','videoId3'=>'','videoId4'=>''];
        $videossw = ['vidname'=>'','vidname1'=>'','vidname2'=>'','vidname3'=>'','vidname4'=>''];
        $videoss = ['publishedAt'=>'','publishedAt1'=>''];

        $i = 0;
        //if(isset($videoList[$i]["id"]["videoId"])) {
        foreach($json['items'] as $items)
        {
            $keyy  = 0==$i ? 'publishedAt' : 'publishedAt'.$i;
            $videoss[$keyy] = $items['snippet']['publishedAt'];

            $keyyy  = 0==$i ? 'vidname' : 'vidname'.$i;
            $videossw[$keyyy] = $items['snippet']['title']; 

            $key  = 0==$i ? 'videoId' : 'videoId'.$i;
            $videos[$key] = $items['id']['videoId']; 
            ++$i;
        }
        $qqq = 'INSERT INTO users(channelid, publishedAt, publishedAt1, videoId, videoId1, videoId2, videoId3, videoId4, vidname, vidname1, vidname2, vidname3, vidname4) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE channelid=?, publishedAt=?, publishedAt1=?, videoId=?, videoId1=?, videoId2=?, videoId3=?, videoId4=?, vidname=?, vidname1=?, vidname2=?, vidname3=?, vidname4=?';
        $stmt = $conn->prepare($qqq);
        $stmt->bind_param('ssssssssssssssssssssssssss', $channelid, $videoss['publishedAt'], $videoss['publishedAt1'], $videos['videoId'], $videos['videoId1'], $videos['videoId2'], $videos['videoId3'], $videos['videoId4'], $videossw['vidname'], $videossw['vidname1'], $videossw['vidname2'], $videossw['vidname3'], $videossw['vidname4'], $channelid, $videoss['publishedAt'], $videoss['publishedAt1'], $videos['videoId'], $videos['videoId1'], $videos['videoId2'], $videos['videoId3'], $videos['videoId4'], $videossw['vidname'], $videossw['vidname1'], $videossw['vidname2'], $videossw['vidname3'], $videossw['vidname4']);

        $stmt->execute();

    }
oxalkeyp

oxalkeyp1#

之后写一个条件

foreach ($json['items'] as $items) {

这就取消了数据的资格。

if ($items['id']['kind'] != 'youtube#playlist') {

实施:

foreach ($json['items'] as $i => $items) {
    if ($items['id']['kind'] != 'youtube#playlist') {
        $keyy = !$i ? 'publishedAt' : "publishedAt$i";
        $videoss[$keyy] = $items['snippet']['publishedAt'];

        $keyyy = !$i ? 'vidname' : "vidname$i";
        $videossw[$keyyy] = $items['snippet']['title']; 

        $key = !$i ? 'videoId' : "videoId$i";
        $videos[$key] = $items['id']['videoId']; 
    }
}

相关问题