simonp Posted February 20, 2010 Share Posted February 20, 2010 Hi, I have an array such as: $array['name'] $array['gender'] $array['sex'] ... and I want to sort the output by the sex field If I use sort($array) it sorts by the name field. If I use sort($array['sex']) I get an errror: Warning: sort() expects parameter 1 to be array, null given in /home Any ideas? Cheers Simon Link to comment https://forums.phpfreaks.com/topic/192713-sorting-an-array/ Share on other sites More sharing options...
gizmola Posted February 20, 2010 Share Posted February 20, 2010 Would need to see your assignment code, but clearly, $array['sex'] is not an array, so you probably have more issues than trying to get a sort. With that said, arsort is probably something that could help you, but given your current structure. Understanding more about your application would be helpful in suggesting whether you're heading down a dead end. Link to comment https://forums.phpfreaks.com/topic/192713-sorting-an-array/#findComment-1015171 Share on other sites More sharing options...
simonp Posted February 20, 2010 Author Share Posted February 20, 2010 Sorry gizmola - I was trying to simplify things and probably made them more complicated Here's the code I'm working with: $json = '[{"domain":"testdomain","tld":".net","result":"Taken"},{"domain":"testdomain","tld":".com","result":"Taken"},{"domain":"testdomain","tld":".mobi","result":"Taken"},{"domain":"testdomain","tld":".biz","result":"Taken"},{"domain":"testdomain","tld":".info","result":"Taken"}]'; $data = json_decode($json, TRUE); foreach ($data as $domain) { echo $domain['domain'] . $domain['tld'] . ' is ' . $domain['result'] . '<br>'; } What I want to do is sort the results by the $domain['result'] (ie have the Available's first then the Taken's Hope you can help. Thanks Simon Link to comment https://forums.phpfreaks.com/topic/192713-sorting-an-array/#findComment-1015173 Share on other sites More sharing options...
perseadrian Posted February 21, 2010 Share Posted February 21, 2010 Hi, I have an array such as: $array['name'] $array['gender'] $array['sex'] ... and I want to sort the output by the sex field If I use sort($array) it sorts by the name field. If I use sort($array['sex']) I get an errror: Warning: sort() expects parameter 1 to be array, null given in /home Any ideas? Cheers Simon You can't sort this type of array. Basically only PHP can use this kind of specification. $array['name'], in other programs it's not posible. You must have an integer index. It would work, otherwise you must make a function to sort array.(booble sort ) Link to comment https://forums.phpfreaks.com/topic/192713-sorting-an-array/#findComment-1015654 Share on other sites More sharing options...
simonp Posted February 24, 2010 Author Share Posted February 24, 2010 Sorry gizmola - I was trying to simplify things and probably made them more complicated Here's the code I'm working with: $json = '[{"domain":"testdomain","tld":".net","result":"Taken"},{"domain":"testdomain","tld":".com","result":"Taken"},{"domain":"testdomain","tld":".mobi","result":"Taken"},{"domain":"testdomain","tld":".biz","result":"Taken"},{"domain":"testdomain","tld":".info","result":"Taken"}]'; $data = json_decode($json, TRUE); foreach ($data as $domain) { echo $domain['domain'] . $domain['tld'] . ' is ' . $domain['result'] . '<br>'; } What I want to do is sort the results by the $domain['result'] (ie have the Available's first then the Taken's Hope you can help. Thanks Simon Is it *really* not possible to do this sort?! Link to comment https://forums.phpfreaks.com/topic/192713-sorting-an-array/#findComment-1017338 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.