...

View Full Version : Comparing numbers differently



darksen
05-18-2011, 01:06 PM
I spent several hours searching on how I can compare numbers

ex : 5-6-8-7 with 6-7-5-8

the results should be the same since the numbers are the same. My logic is to sort first the number then compare it. can anyone help me on how to sort this number that has dash on it ?

Got this from the net but I dont know how to pass the numbers to the array without hard cording it. Assuming this will be inputted on form.

$array = array(5, 6, 8, 7);
natsort($array); // Sorts from highest to lowest
for ($i = 0; $i < 4; $i++)
{
echo $array[$i];
}

mlseim
05-18-2011, 01:30 PM
Try this test script


<?php
$string="5-6-8-7";

// this creates an array, separating the string by dashes.
$numbers=explode("-",$string);

// reverse sort;
rsort($numbers);

foreach($numbers as $number){
echo "$number <br>";
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum