Array push does not push the whole items of an array


This Content is from Stack Overflow. Question asked by japose7523

I am reading a json file like this:

 $path = storage_path() . "/userexam.json"; 
 $json = json_decode(file_get_contents($path), true); 

 foreach($json as $js){

And the result of dd($js) which shows every single element of json goes like this:

array:13 [▼
  "_id" => "d4466d06-3d47-41c4-aa5e-664752d84004"
  "user" => "0123456789"
  "exam" => "First Exam"
  "total_time_in_minute" => 8640
  "number_of_question" => 17
  "start_time" => array:1 [▶]
  "questions" => array:17 [▶]
  "last_question" => 10
  "is_finished" => false
  "result" => null
  "updated_at" => array:1 [▶]
  "created_at" => array:1 [▶]
  "score" => null

Now I want to push these elements into a single array.

So I did this:

$array_results = [];
foreach($json as $js){
    array_push($array_results, $js);

But now the results of dd($array_results) goes like this:

enter image description here

As you can see it does not show the sub-arrays start_time, questions, updated_at and created_at and only a count number exists.

So how can I push the whole arrays into a new one while they’re sub-arrays also exist in the new array?


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?