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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 ) Quote Link to comment 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?! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.