返回里面foreach php和laravel [英] return inside foreach php and laravel

查看:78
本文介绍了返回里面foreach php和laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I trying to return data every time inside the foreach() but i keep on getting the data on the first iteration of the loop here is my code

  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的数据

on the var_dump($i); It only shows data for the first 0 only.So how can i return the data also for 1,2,3

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

here is my output when pr($data) for 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',
     )),
    ),

推荐答案

return运算符

The return operator implicitly ends the current execution scope. The context of your usage is not given, but you could put your $data into an array prior to JSON encoding and returning it. It might look something like this:

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

这篇关于返回里面foreach php和laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆