Home > Mobile >  How to add a new item to every index in the array -php?
How to add a new item to every index in the array -php?

Time:12-17

var_export($res) is an array as below.

array(0 =>(object) array('user' => NULL,
                        'courseId' => 18,),
      1 =>(object) array('user' =>(object) array('id' => 1,
                                                'name' => 'admin',
                                                'files' => NULL,),
                        'courseId' => 1,),
    )

At every index in this array, i need to calculate count using the courseId and add a new item called count to every index. i used the below code. And the expected end result is an array of objects , not an object of objects.

$res=json_decode($response);
foreach ($res as $key ) {                   
    $count = MyCourse::where('course_id', $key->courseId)->distinct('student_id')->count();
    $res['count'] = $count; 
}
return response()->json(['data' => $res,'statusCode' => 200], 200);

the above code shows the below data. it added count as a new index in the array not added it as anew item to every index of the array. Also, it is returning an result in the form of object of objects.How can i fix this?

    {
        "0": {
            "user": null,
            "courseId": 18
        },
        "1": {
            "user": {
                "id": 1,
                "name": "admin",
                "files": null
            },
            "courseId": 1
        },
        "count": 1
    }

Expected End Result:

  [
       {
            "user": null,
            "courseId": 18,
            "count": 20
        },
        {
            "user": {
                "id": 1,
                "name": "admin",
                "files": null
            },
            "courseId": 1,
            "count": 10
        }
  ]

CodePudding user response:

In your foreach loop, $key is each of your objects containing courseId.

Instead of adding count to $res, add it to $key like so:

foreach ($res as &$key ) {                   
    $count = MyCourse::where('course_id', $key->courseId)->distinct('student_id')->count();
    $key->count = $count; // set it to $key instead of $res
}

If you want the results to be arrays instead of objects, pass true as the second argument to json_decode like so:

$res = json_decode($response, true); // associative array instead of object

and then edit the foreach to handle it as arrays instead:

foreach ($res as $key ) {                   
    $count = MyCourse::where('course_id', $key['courseId'])->distinct('student_id')->count();
    $key['count'] = $count; // set it to $key instead of $res
}

CodePudding user response:

In your case you want to set the count field for $key instead of $res

Your foreach loop should look like this:

foreach ($res as $key ) {                   
    $count = MyCourse::where('course_id', $key->courseId)->distinct('student_id')->count();
    $key->count = $count; 
}

This would output:

    {
        "0": {
            "user": null,
            "courseId": 18,
            "count": 20
        },
        "1": {
            "user": {
                "id": 1,
                "name": "admin",
                "files": null
            },
            "courseId": 1,
            "count": 10
        }
    }
  • Related