Unexpected object format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wellmoon
Forum Newbie
Posts: 4
Joined: Sun Apr 19, 2009 3:04 am

Unexpected object format

Post by wellmoon »

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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Unexpected object format

Post by requinix »

Using array_keys should help you fix it.
That's supposed to be array_values instead. Not sure how I got them backwards.

Pretty obvious that usort is doing it so it probably has something to do with how it's sorted. It might sort on the actual array itself by creating new elements then deleting the old elements at the end.
Last edited by requinix on Sun Apr 19, 2009 3:26 pm, edited 1 time in total.
wellmoon
Forum Newbie
Posts: 4
Joined: Sun Apr 19, 2009 3:04 am

Re: Unexpected object format

Post by wellmoon »

thanks for the replay tasairis, yes the keys are generated by the uasort function and appear to be the original index number of each item prior to being sorted.

I tried using array_keys in various places but I couldn't get it to remove these keys.

No worries, like I say it's not a biggie and I can code around it anyway...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Unexpected object format

Post by requinix »

Er, sorry, I meant to say array_values. It'll return the same data but it'll reindex the keys. json_encode should treat it like a non-associative array then.
wellmoon
Forum Newbie
Posts: 4
Joined: Sun Apr 19, 2009 3:04 am

Re: Unexpected object format

Post by wellmoon »

Cool, yeah I can see that working, I'll give it a try, thanks :D
wellmoon
Forum Newbie
Posts: 4
Joined: Sun Apr 19, 2009 3:04 am

Re: Unexpected object format

Post by wellmoon »

Just to confirm, array_values has resturned the JSON object to its original structure :D

thanks
Post Reply