Imran Abbas
Imran Abbas

Reputation: 805

Foreach loop does not storing data into nested array

Hi I am trying to store data into nested arrays. I am using laravel and I have following query

$posts = User::select(DB::raw('first_name, email, left(DATE(created_at),10) as registeredDate'))
->whereHas('roles', function($q){
$q->where('name', '=', 'agent');
})
->offset(0)
->limit(5)
->orderBy('created_at','DESC')
->get();

After getting result from query I need to sort it group by registeredDate

$grouped_users = $posts->groupBy('registeredDate');

Now here I need to store the result into array and then nested array

if($grouped_users){
    $index = 0;
    foreach($grouped_users as $r){
        $nestedData['id'] = $index;
        $nestedData['counter'] = sizeof($r);
        $nestedData['start_date'] = $r[0]->registeredDate;
        $ChildIndex = 0;
        foreach($r as $cr){
            $nestedData['nested_data'][] = array(
                'full_name' => $cr->first_name,
                'last_login' => date('d M Y',strtotime($cr->registeredDate)),
                'email' => $cr->email,
            );
            $ChildIndex++;
        }
        $data[] = $nestedData;
        $index++;
    }
}

The above loop is storing previous nested array data into current nested array.

You can see the example below result which I have converted into json

{
  "data": [
    {
      "id": 0,
      "counter": 3,
      "start_date": "2019-01-07",
      "nested_data": [
        {
          "full_name": "Nicolas",
          "last_login": "07 Jan 2019",
          "email": "[email protected]"
        },
        {
          "full_name": "michel",
          "last_login": "07 Jan 2019",
          "email": "[email protected]"
        },
        {
          "full_name": "Yann",
          "last_login": "07 Jan 2019",
          "email": "[email protected]"
        }
      ]
    },
    {
      "id": 1,
      "counter": 2,
      "start_date": "2019-01-05",
      "nested_data": [
        {
          "full_name": "Nicolas",
          "last_login": "07 Jan 2019",
          "email": "[email protected]"
        },
        {
          "full_name": "michel",
          "last_login": "07 Jan 2019",
          "email": "[email protected]"
        },
        {
          "full_name": "Yann",
          "last_login": "07 Jan 2019",
          "email": "[email protected]"
        },
        {
          "full_name": "Armin",
          "last_login": "05 Jan 2019",
          "email": "[email protected]"
        },
        {
          "full_name": "Peter",
          "last_login": "05 Jan 2019",
          "email": "[email protected]"
        }
      ]
    }
  ]
}

Can someone kindly guide how can I fix the issue and prevent to store again nested array. Thanks in advance.

Upvotes: 1

Views: 317

Answers (2)

geseq
geseq

Reputation: 26

PHP will continue using the same $nestedData variable for every iteration of the loop. Clear an empty nested array within your first loop before you proceed:

foreach($grouped_users as $r){
    $nestedData = [];

Upvotes: 1

nacho
nacho

Reputation: 5396

You need to "delete" nestedData array each time before the foreach. Something like this:

$nestedData['nested_data']=array()
foreach($r as $cr){

Upvotes: 0

Related Questions