Hi Nogdog, thanks for that!
Unfortunately the double loop seems to be a lot quicker than the usort function. Also I don't know why but it is getting the order wrong. Run the bit of code below to see what I mean.
<?php
$order = array('A' => '', 'K' => 'K', 'Q' => 'Q', 'J' => 'JJJ', 'T' => '', '9' => '', '8' => '', '7' => '', '6' => '', '5' => '', '4' => '', '3' => '3', '2' => '');
#test one
function mysort($a, $b)
{
$diff = strlen($b) - strlen($a);
if($diff === 0) { $diff = 1; } // maintain original order
return($diff);
}
microtime(true); #initialise
$result = 0;
for($i = 0; $i < 1000; $i++)
{
$test = $order;
$s = microtime(true);
usort($test, "mysort");
$e = microtime(true);
$result += ($e - $s);
}
$string = substr(implode('', $test), 0, 5);
echo '<h3>Nogdog</h3>'.$string;
echo '<br>';
echo round((($result*1000000)/($i+1))-2, 2).' microseconds<br>';
#test two
$result = 0;
for($i = 0; $i < 1000; $i++)
{
$s = microtime(true);
$newOrder = '';
for($k = 4; $k > 0; $k--)
{
foreach($order as $card)
if(strlen($card) == $k)
{
$newOrder .= $card;
if(strlen($newOrder)>4)
{
break(2);
}
}
}
$e = microtime(true);
$result += ($e - $s);
}
echo '<h3>Original</h3>'.substr($newOrder, 0, 5);
echo '<br>';
echo round((($result*1000000)/($i+1))-2, 2).' microseconds<br>';
?>