Currently I’m attempting to simulate hours of the day with my keys for inserting into Mongo and I have something along these lines (simplified) setup:
for($i=0;$i<23;$i++){
$returnElement["hours"]["$i"] = array();
foreach($this->list_of_daily_usage_items as $item){
/* @var $item DailyUsageItem */
$returnElement["hours"]["$i"][$item->getLabel()] = $item->getDefaultValue();
}
}
With this I’m creating something similar to:
[hours] => Array
(
[0] => Array
(
[labelOne] => 0
[labelTwo] => 0
)
....
[23] => Array
(
[labelOne] => 0
[labelTwo] => 0
)
)
However then when i push to JSON It’s converting that to:
"hours": [{
"labelOne": 0,
"labelTwo": 0,
}, {
"labelOne": 0,
"labelTwo": 0,
}]
Which normally I would expect; however when I’m attempting to make these numeric keys it’s not really what I’m hoping to see..
Is there a way to achieve this short of prepending something like _ to the number to force it to a string?
Thank you
You could add a temporary element to force a PHP Array to be converted a JSON Object
this will generate
If we combine this with a neato regexp, we just hassled us past PHPs somewhat evil type system:
which gives us this: