Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
PHP has shorthand for pushing onto an array: $array[] = $var_to_push –  G-Nugget Feb 12 '13 at 21:28

1 Answer 1

Just get rid of the $full_array and do:

echo json_encode($results_array);
share|improve this answer
    
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. –  Austin Jenkins Feb 12 '13 at 21:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.