0

I'm trying to generate a multi-layered JSON file using php's array function (for 3 separate arrays). I'm essentially using 3 different arrays in php to generate the one Here is the basic code I am using in php:

$full_array = array();
...
$results_array('step1');
foreach($ga->getResults() as $result):
     $add_array = array();
     $add_array['total'] = $ga->getResults();
     array_push($results_array, $add_array);
endforeach;
array_push($full_array, $results_array);
echo json_encode($full_array)

This results in the following:

[
   [
      "step1",
         {
            "total": "...."
         }
   ]
]

However, I'm trying to get that "step1" before the brace, so it should look like the following:

[
   "step1",
      [
           {
             "total": "...."
           }
      ]
]

Does anybody know how to get the latter format using PHP? I've searched for this answer everywhere but could not find it anywhwere - apologies if there is a duplicate answer. Any help would be greatly appreciated.

1
  • PHP has shorthand for pushing onto an array: $array[] = $var_to_push Commented Feb 12, 2013 at 21:28

1 Answer 1

0

Just get rid of the $full_array and do:

echo json_encode($results_array);
1
  • Thanks Jeroen & G-Nugget. I guess I should have elaborated, the reason I hae the $full_array is that there are multiple loops that I will be using to push into the $full_array. Commented Feb 12, 2013 at 21:52

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.