Unexpected object format
Posted: Sun Apr 19, 2009 3:12 am
Hi, I've got some code which works and does what I want, it's just doing it in a way that I'd like to change if possible...
I'm working with a multidimensional array, where each item in the array is an associative array. In each associative array there is an item which has a numerical value.
I'm then using the following code to sort the outer array so that the inner arrays are in descending order based on this numerical value:
// Comparison function
function cmp($a, $b) {
return ($a["numeric"] > $b["numeric"]) ? -1 : 1;
}
// Sort and print the resulting array
uasort($array, "cmp");
Once the array has been sorted I'm then using the json_encode function to convert the array to a JSON object. Like I say it's working perfectly, it's just the structure of the resulting JSON object is a little weird. If I comment out the above code and don't sort the array, my JSON object looks like this:
[
{"item1":"a string value","item2":"another string","numeric":3},
{"item1":"a string value","item2":"another string","numeric":1}
]
But with the sort code working the object changes to this:
{
"2":{
"item1":"a string value","item2":"another string","numeric":3
},
"3":{
"item1":"a string value","item2":"another string","numeric":1
}
}
For some reason, the object has keys with random numerical strings (they don't seem to be related to my numeric value) and the data I want as values. I can still access the data I want so I'm not overly bothered about it, but I'd like to know why it's happening and whether I can turn my JSON object back into a JSON array if possible?
thanks
I'm working with a multidimensional array, where each item in the array is an associative array. In each associative array there is an item which has a numerical value.
I'm then using the following code to sort the outer array so that the inner arrays are in descending order based on this numerical value:
// Comparison function
function cmp($a, $b) {
return ($a["numeric"] > $b["numeric"]) ? -1 : 1;
}
// Sort and print the resulting array
uasort($array, "cmp");
Once the array has been sorted I'm then using the json_encode function to convert the array to a JSON object. Like I say it's working perfectly, it's just the structure of the resulting JSON object is a little weird. If I comment out the above code and don't sort the array, my JSON object looks like this:
[
{"item1":"a string value","item2":"another string","numeric":3},
{"item1":"a string value","item2":"another string","numeric":1}
]
But with the sort code working the object changes to this:
{
"2":{
"item1":"a string value","item2":"another string","numeric":3
},
"3":{
"item1":"a string value","item2":"another string","numeric":1
}
}
For some reason, the object has keys with random numerical strings (they don't seem to be related to my numeric value) and the data I want as values. I can still access the data I want so I'm not overly bothered about it, but I'd like to know why it's happening and whether I can turn my JSON object back into a JSON array if possible?
thanks