doutandusegang2961 2017-02-08 13:02 采纳率: 0%
浏览 216

递归函数返回数组始终为null

Trying to return an array of items from api call, the var_dump spits out the array that I want, but the return is always null. What am I missing? Many thanks

function get_all_opened($mailgun, $domain, $list_address, $items, $page_advance = false, $next_url = false) 
{

    $end_point = "{$domain}/events";
    if($page_advance) {
        $end_point = "{$domain}/events/{$next_url}";
    }

    //API Call
    $stats = $mailgun->get($end_point, array(
        'event' => 'opened',
        'limit' => 25,
        'list' => $list_address
    ));

    $item_count = count($stats->http_response_body->items);

    if($item_count > 0) {
        $items[] = $stats->http_response_body->items;
    }
    if($item_count < 25) {
        var_dump($items); //Correct result set
        return $items; //Always NULL
    }

    if($item_count > 24) {
        $next_parts = explode ('/', $stats->http_response_body->paging->next);
        $next_url = end($next_parts);
        get_all_opened($mailgun, $domain, $list_address, $items, true, $next_url);
     }
}

$items = array();
get_all_opened($mailgun, $domain, $list_address, $items);
  • 写回答

1条回答 默认 最新

  • dqq22391 2017-02-08 14:11
    关注

    Fixed and working function below

    function get_all_opened($mailgun, $domain, $list_address, $items, $page_advance = false, $next_url = false) {
    
      $end_point = "{$domain}/events";
      if($page_advance) {
        $end_point = "{$domain}/events/{$next_url}";
      }
    
      //API Call
      $stats = $mailgun->get($end_point, array(
          'event' => 'opened',
          'limit' => 25,
          'list' => $list_address
      ));
    
    
      $item_count = count($stats->http_response_body->items);
    
    //add items to return array
      if($item_count > 0) {
        $items[] = $stats->http_response_body->items;
      }
    
    
      if($item_count > 24) {
        $next_parts = explode ('/', $stats->http_response_body->paging->next);
        $next_url = end($next_parts);
        return get_all_opened($mailgun, $domain, $list_address, $items, true, $next_url);
      } else {
        return $items;
      }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料