www.webdeveloper.com
Results 1 to 6 of 6

Thread: how to sort string with number inside?

  1. #1
    Join Date
    Mar 2007
    Posts
    101

    how to sort string with number inside?

    how to sort a string with a number inside the string?
    ex.
    $arr[0]="a1";
    $arr[0]="a3";
    $arr[0]="a20";
    $arr[0]="a9";

    when i use sort array or usort arrauy the out put is like this

    out: a1,a20,a3,a9

    it will going to desplay first the "a20" instead of "a3"... i want to sort them like this Out: a1,a3,a9,a25...

    if somebody knows how to do that please help me... i really2x need it.. please help me..

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    Take a look at the natsort() function. I believe it will do what you're looking for.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2007
    Posts
    101
    sorry my sample was wrong all keys is zero it should 0-3.. yah... i see that natsort but it gues like this when printing

    $arr[0]="a1";
    $arr[1]="a3";
    $arr[2]="a20";
    $arr[3]="a9";

    output:
    $arr[0]="a1";
    $arr[1]="a3";
    $arr[3]="a9";
    $arr[2]="a20";

    but the problem is the keys its because the keys still in that value

    because i want to desplay the data in combo box which i use loop and keys of the array when desplaying it. i use ajax with this to retrieve from php.. is there anyway to do this?
    Last edited by johnix; 03-14-2008 at 10:43 AM.

  4. #4
    Join Date
    Mar 2007
    Posts
    101
    sorry my sample was wrong all keys is zero it should 0-3.. yah... i see that natsort but it gues like this when printing

    $arr[0]="a1";
    $arr[1]="a3";
    $arr[2]="a20";
    $arr[3]="a9";

    output:
    $arr[0]="a1";
    $arr[1]="a3";
    $arr[3]="a9";
    $arr[2]="a20";

    but the problem is the keys its because the keys still in that value

    because i want to desplay the data in combo box which i use loop and keys of the array when desplaying it. i use ajax with this to retrieve from php.. is there anyway to do this?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    PHP Code:
    <?php
    $arr
    [0]="a1";
    $arr[1]="a3";
    $arr[2]="a20";
    $arr[3]="a9";
    echo 
    "<pre>Before:\n".print_r($arr,1)."\n";
    natsort($arr);
    $arr array_values($arr); // <---------------------
    echo "After:\n".print_r($arr,1)."</pre>";
    Output:
    Code:
    Before:
    Array
    (
        [0] => a1
        [1] => a3
        [2] => a20
        [3] => a9
    )
    
    After:
    Array
    (
        [0] => a1
        [1] => a3
        [2] => a9
        [3] => a20
    )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Mar 2007
    Posts
    101
    wow... thank you very much nogdog... this really help me alot....

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles