Jump to content

Sorting an array


simonp

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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 :P)

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.