【问题标题】:Recursive function in PHP function : how to prevent return value?PHP函数中的递归函数:如何防止返回值?
【发布时间】:2015-08-23 21:26:53
【问题描述】:

我正在开发一个 API 来获取数据,就像其他标准 API 一样,它限制了 item 的返回数,例如在我的情况下,每页仅返回 1 项。所以,我必须得到

  1. total_count,
  2. page_size(在我的情况下是 1),
  3. 和 current_page_number。

问题是,无论我将其设置为 recursive ,它仍然会在第一个周期返回值。例如如果总共有 3 个项目,并且每页限制为 1 个项目,它将在第一个循环期间返回该值。

为什么以及如何解决这个问题?

非常感谢

function get_video_list($page_no = 0, $filter_video = array()) {
    $api = "http://api.brightcove.com/services/library?command=search_videos&page_size=1&video_fields=id%2Cname%2CcreationDate%2CFLVURL%2CpublishedDate%2ClinkURL%2CthumbnailURL%2Clength&media_delivery=http&sort_by=CREATION_DATE%3AASC&page_number=$page_no&get_item_count=true&token=" . READ_TOKEN;

    try {
        $ch = curl_init();

        if (FALSE === $ch) {
            throw new Exception('failed to initialize');
        }

        curl_setopt($ch, CURLOPT_URL, $api);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $content = curl_exec($ch);

        if (FALSE === $content) {
            throw new Exception(curl_error($ch), curl_errno($ch));
        }

        $video_list = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);

        $page_number = ($video_list->page_number) + 1; //start at 0
        $page_size = $video_list->page_size;
        $total_count = $video_list->total_count;

        foreach ($video_list->items as $video) {
            if (in_array($video->id, $stored_video)) {
                $filter_video [] = $video;
            }
        }

        if ($total_count > $page_size * $page_number) {
            get_video_list($page_number, $filter_video);
        }

        return $filter_video;
    } catch (Exception $e) {
        trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
    }
}

【问题讨论】:

  • 您正在检查$video->id 是否在$stored_video 数组中,但是甚至没有定义这样的值。

标签: php function recursion return return-value


【解决方案1】:

你没有将递归调用的返回值赋给$filter_video,改为:

if ($total_count > $page_size * $page_number) {
    $filter_video = get_video_list($page_number, $filter_video);
}

或者,更好的是:通过引用传递。这完全消除了对返回值的需求,特别适用于递归函数(请注意函数声明中的 &$filter_video,请参阅http://php.net/manual/en/language.references.pass.php)。

function get_video_list($page_no = 0, &$filter_video = array()) {
    $api = "http://api.brightcove.com/services/library?command=search_videos&page_size=1&video_fields=id%2Cname%2CcreationDate%2CFLVURL%2CpublishedDate%2ClinkURL%2CthumbnailURL%2Clength&media_delivery=http&sort_by=CREATION_DATE%3AASC&page_number=$page_no&get_item_count=true&token=" . READ_TOKEN;

    try {
        $ch = curl_init();

        if (FALSE === $ch) {
            throw new Exception('failed to initialize');
        }

        curl_setopt($ch, CURLOPT_URL, $api);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $content = curl_exec($ch);

        if (FALSE === $content) {
            throw new Exception(curl_error($ch), curl_errno($ch));
        }

        $video_list = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);

        $page_number = ($video_list->page_number) + 1; //start at 0
        $page_size = $video_list->page_size;
        $total_count = $video_list->total_count;

        foreach ($video_list->items as $video) {
            if (in_array($video->id, $stored_video)) {
                $filter_video [] = $video;
            }
        }

        if ($total_count > $page_size * $page_number) {
            get_video_list($page_number, $filter_video);
        }
    } catch (Exception $e) {
        trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
    }
}

然后这样称呼它:

$myVideoList = [];
get_video_list(0, $myVideoList);

// Do stuff with $myVideoList

【讨论】:

    【解决方案2】:

    这应该可以解决您的问题。

    if ($total_count > $page_size * $page_number) {
          return get_video_list($page_number, $filter_video);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-11
      • 2021-03-02
      • 2011-04-20
      • 2013-09-21
      • 2012-11-27
      • 1970-01-01
      • 2011-05-23
      • 2015-02-04
      • 2022-11-05
      相关资源
      最近更新 更多