Jump to content

[SOLVED] Natsort Failing Meh


StormTheGates

Recommended Posts

Can someone explain to me why this dosnt work?

 

$cards = array("2h", "4d", "1s", "5c", "8d");
natsort($cards);
echo "$cards[0]-$cards[1]-$cards[2]-$cards[3]";

 

You would think that it would sort them into 1s-2h-4d-5c

 

But it dosnt, it dosnt seem to sort at all. It just prints out

 

2h-4d-1s-5c

 

Any ideas why? Its driving me insane.

Link to comment
Share on other sites

Guest prozente

It's working just as stated. From the PHP manual

 

"This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations."

 

print_r ($cards);

 

and you will see it did sort, it doesn't reindex.

 

If you want to re-index you can use array_values

 

$cards = array_values($cards);

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.