【问题标题】:PHP youtube API v3 cant unset array keyPHP youtube API v3 无法取消设置数组键
【发布时间】:2015-12-29 18:41:38
【问题描述】:
      require_once ('../google-api-php-client/src/Google_Client.php');
      require_once ('../google-api-php-client/src/contrib/Google_YouTubeService.php');        
                   try {
                    $searchResponse = $youtube->search->listSearch('id,snippet', array(
                      'q' => $value,
                      'maxResults' => $maxRes,
                      'videoCategoryId' => '10',
                      'type' => 'video'
                    ));

                  foreach ($searchResponse['items'] as $key => $searchResult) {
                      switch ($searchResult['id']['kind']) {
                        case 'youtube#video':

              unset($searchResult[$key]);

                  .....

这是 youtube api v3 搜索列表的输出。

[items] Array
        (
            [0] Array
                (

             )
            [1] Array
                (

             )
          etc...

无法取消设置 youtube 项目数组?

EG: unset($searchResponse['items'][$key]); 仍然输出该视频。 我做错了什么?谢谢。

【问题讨论】:

    标签: php arrays youtube


    【解决方案1】:

    当您迭代 $searchResponse['items'] 时,应该从这个数组中删除项目:

    unset($searchResponse['items'][$key]);
    

    【讨论】:

    • 抱歉不工作已经试过了。甚至未设置($searchResponse);不工作。
    【解决方案2】:

    目前,您的代码仅取消设置本地 $searchResult 变量的 $key 偏移量,该变量仅存在于您的 foreach 循环中。您需要通过调用 unset($searchResponse['items'][$key]) 来取消设置,或者将循环更改为读取 foreach ($searchResponse['items'] as $key => &$searchResult)

    【讨论】:

    • 抱歉不工作已经试过了。甚至未设置($searchResponse);不工作。
    • 这段代码是在函数中还是在某种方法中?
    • 这是 youtube API v3。 link 。它的内部函数
    • $searchResponse是在函数内部创建的,还是通过引用/全局传递给它的?
    • 在函数内部,你在我的问题中没有看到?
    猜你喜欢
    • 2016-04-10
    • 2021-05-19
    • 2014-01-26
    • 2014-08-25
    • 2014-08-13
    • 1970-01-01
    • 2019-07-28
    • 2016-08-04
    • 1970-01-01
    相关资源
    最近更新 更多