2

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);


    }

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

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',
     )),
    ),
2
  • How about storing all the received data in an array first and after the for loop you can return it Commented May 28, 2015 at 20:58
  • let me show my out put on pr($data) Commented May 28, 2015 at 21:00

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

5 Comments

i want the data as json because i want to use the data somewhere
@ReginwaldtLed - Updated my answer according to your new information given about context + added example.
i get this error __clone method called on non-object
@ReginwaldtLed - Oh, I see that your data is nested arrays in your update. If you remove clone what does var_dump($data) show after your foreach is complete, but before return?
@fantascticrice nested arrays for the last record only
0

You can take this example in laravel Here I am returning multiple Prices

$prices = $this->Prices;
foreach ($prices as $price) {

    $res[] = [
        'travel_mode_id' =>$price->travel_mode_id,
        'occupancy_id' => $price->occupancy_id,
        'rider_id' => $price->occupancy_id,
        'price' => $price->price,

    ];
}
return $res;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.