Results 1 to 5 of 5

Thread: sorting arrays

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,742
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default sorting arrays

    It's a little more complicated than it sounds.

    $key[anderson]=12000045value;
    $key[mike]=12000047reverse;
    $key[tom]=21000047absent;

    how do you sort by character#7 on to the end of the value in alphanumeric order? I want to ignore the first 6 characters of the value of the arrays. I've thought of removing the first 6 characters and reinserting it back into the value after it has been sorted, but just can't seem to wrap my mind around it.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,742
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    figured it out. It was complicated too, because I had to insert the array keys into the value between the numbers and the text as text and remove it after the array was sorted and the split arrays were spliced back together. I'll try to post the code a bit later. The code is short and not too complicated, but figuring out how to do it was.
    Code:
    $key[anderson]=12000045value;
    $key[mike]=12000047reverse;
    $key[tom]=21000047absent;
    foreach ($key as $name => $row1){}
    Code:
    $new=array();$new1=array();$new2=array();$row3=array();
    $count_n=count($name);$a=0;
    while ($a<=$count_n)
    {$new["$name[$a]"]=$row1[$a];
    $a++;}
    foreach ($new as $name1 => $row2)
    {
    $row3=substr("$row2",0,5);
    $row2=substr("$row2",5);
    $row2=substr_replace($row2,$name1,3,0);
    $new["$name1"]=$row2;
    $new1["$name1"]=$row3;
    }
    asort($new);
    foreach($new as $name1 => $row2)
    {$row2=str_replace("$name1","",$row2);$new["$name1"]="$new1[$name1]$row2";}
    In the first foreach I separated $key into two arrays: one for the keys and one for the values.

    In the while statement (which I'll probably change to a foreach since I no longer need to avoid certain parts of the array) I started a new array which is a duplicate of the original array.

    In the second foreach statement I split the duplicate array $new, removed the first 5 characters of the value and inserted the key name into the value between the numbers and the text, and saved the 5 removed digits for later.

    Next I sorted the array, which will now sort the array by value and then by key.
    In the last foreach the key names are removed from the values where they were inserted and the first 5 numbers that were removed earlier are put back where they were thus returning the values to their original value.

    I hope that wasn't too complicated. I am pretty sure that I can simplify it further, but the above code was the first working version I came up with. The above code is pretty close to what I am using, but with a few minor changes.

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    PHP Code:
    <?php
    usort
    ($arraycreate_function('$a,$b','return strcmp(substr($a,7), substr($b,7));'));
    ?>
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,742
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    see, now it's code like that that makes me wish I knew how to create functions or read them. Oh, well. You did in one line what I did in around 20 or so .

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Actually, this is more versatile:
    PHP Code:
    <?php
    function offset_sort($array,$n=0) {
       
    usort($arraycreate_function('$a,$b','return strcmp(substr($a,'.$n.'), substr($b,'.$n.'));'));
    }
    offset_sort($array,7);
    ?>

    By the way, this isn't that complex, actually. usort uses a user defined function. I could just write the function out (the first argument is the variables $a, $b; the second is the php source of the function), and then just call it using 'function_name' instead of create_function().
    The function itself just uses strcmp(), which compares strings and returns a proper value for usort(), and it includes an offset for the input strings.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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