返回内部的foreach php和laravel

时间:2015-05-28 20:53:40

标签: php laravel-4

我每次都在foreach()内部尝试返回数据但是我继续在循环的第一次迭代中获取数据是我的代码

  for ($i = 0 ;$i<4;$i++)
    {
        var_dump($i);
        $publisher = Publisher::find($results[$i]->publisherId);
        //pr($publisher);
        $channels =$publisher->channels()->get() ;
        pr($channels[0]);
        $data = ReviveAgent::getPublisherDailyStatistics($channels[0],$start,$end);
        pr($data);
        return Response::json($data);


    }

在var_dump($ i)上; 它只显示前0的数据。那么我怎么能为1,2,3

返回数据

这是我的输出,当pr($ data)为var_dump($ i)= 1

            array (

   0 => 
  array (
'impressions' => 1867,
'clicks' => 14,
'requests' => 44,
'revenue' => 2.79,
'day' => 
stdClass::__set_state(array(
   'scalar' => '20150518T00:00:00',
   'timestamp' => 1431907200,
   'xmlrpc_type' => 'datetime',
    )),
   ),
 1 => 
 array (
'impressions' => 2197,
'clicks' => 17,
'requests' => 382,
'revenue' => 19.829999999999998,
'day' => 
stdClass::__set_state(array(
   'scalar' => '20150519T00:00:00',
   'timestamp' => 1431993600,
   'xmlrpc_type' => 'datetime',
   )),
  ),
  2 => 
    array (
 'impressions' => 5484,
'clicks' => 3,
'requests' => 3680,
'revenue' => 6.7300000000000004,
'day' => 
stdClass::__set_state(array(
   'scalar' => '20150520T00:00:00',
   'timestamp' => 1432080000,
   'xmlrpc_type' => 'datetime',
 )),
 ),
 3 => 
 array (
'impressions' => 6909,
'clicks' => 105,
'requests' => 5141,
'revenue' => 378.88499999999999,
'day' => 
stdClass::__set_state(array(
   'scalar' => '20150521T00:00:00',
   'timestamp' => 1432166400,
   'xmlrpc_type' => 'datetime',
     )),
    ),

1 个答案:

答案 0 :(得分:1)

return运算符implicitly ends the current execution scope。我们未提供您使用的上下文,但您可以在{1}}之前将$data置于JSON编码之前并返回array。它可能看起来像这样:

$data = array();
foreach ($i = 0; $i < 4; $i++) {
    ...
    $record = ReviveAgent::getPublisherDailyStatistics($channels[0], $start, $end));
    pr($record);
    $data[] = $record;
}
return Response::json($data);