Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Comparing numbers differently

    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];
    }
    Last edited by darksen; 05-18-2011 at 12:14 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Try this test script
    PHP Code:
    <?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>";
    }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •