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
https://forums.phpfreaks.com/topic/47051-solved-natsort-failing-meh/
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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.