Personally, I'm sure it's a very simple question, but I'm not sure how to do it.
First scenario - I have the following array:
$century = array(
'decade' => array(
array(
'year' => '2017',
'months' => array(
array('value' => 'Jan'),
)
)
)
);
echo "<pre>";
print_r(json_encode($century));
echo "</pre>";
{"decade": [{"year": "2017", "months"
So far so good, the format is like wish, but changing how I create this array and assigns values, tb changes the output to an unwanted format, see below.
Second Scenario - Unwanted format:
$m = array(
array('value' => 'Jan')
);
$century = array(
'decade' => array()
);
$century['decade'][]['year'] = '2017';
$century['decade'][]['months'] = $m[0]['value'];
echo "<pre>";
print_r(json_encode($century));
echo "</pre>";
{"decade": [{"year": "2017"}, {"months": "Jan"}]}
Notice that the year is now surrounded by '{}' individually and not as a whole as in the first scenario.
The question is of course have a right way to do the second scenario to return an output equal to the first, but which?