SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)

    Sequential numbering problems

    Interesting little problem here.
    I am processing large numbers of images and compiling them into a swf file, the images are all sequentially numbered: IM1.jpg IM2.jpg IM3.jpg etc

    When the directory is processed the images are being compiled in a different order.....

    Code:
    1,10,11,12,13,14,15,16,17,18,19,2,20,21,22,23......
    So its doing all the 1's then 2's then 3's.

    Anybody got an idea of how to number the images to stop this?
    Padding with 0's doesnt help nor does alphanumeric numbering.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Natural sorting problem. Padding with 0's should help, however. You must be doing something wrong.

    You could also use natsort().

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Yep, 01 02 03 04 05 06 07 08 09 10 11... should work.
    Post your code

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Interesting indeed, I'll have a pre-lunch stab at it.
    PHP Code:
    <?php
    $images 
    = array(
        
    'img1.jpg',
        
    'img2.jpg',
        
    'img3.jpg',
        
    'img11.jpg',
        
    'img21.jpg',
        
    'img31.jpg',
        
    'img4.jpg',
        
    'img5.jpg',
        
    'img6.jpg',
        
    'img41.jpg',
        
    'img51.jpg',
        
    'img61.jpg'
    );

    $sorted = array();
    foreach(
    $images as $image)
    {
        
    preg_match('~(?<key>[0-9]+)\..+$~'$image$matches);
        
    $sorted[$matches['key']] = $image;
    }
    ksort($sorted);
    print_r($sorted);
    /*
        Array
        (
            [1] => img1.jpg
            [2] => img2.jpg
            [3] => img3.jpg
            [4] => img4.jpg
            [5] => img5.jpg
            [6] => img6.jpg
            [11] => img11.jpg
            [21] => img21.jpg
            [31] => img31.jpg
            [41] => img41.jpg
            [51] => img51.jpg
            [61] => img61.jpg
        )
    */
    ?>
    it needs work, but...
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    A little better.
    PHP Code:
    <?php
    $images 
    = array(
        
    'img1.jpg',
        
    'img2.jpg',
        
    'img3.jpg',
        
    'img11.jpg',
        
    'img21.jpg',
        
    'img31.jpg',
        
    'img4.jpg',
        
    'img5.jpg',
        
    'img6.jpg',
        
    'img41.jpg',
        
    'img51.jpg',
        
    'img61.jpg'
    );

    function 
    sort_callback($a$b)
    {
        
    preg_match('~(?<key>[0-9]+)~'$a$matches);
        
    $a $matches['key'];
        
    preg_match('~(?<key>[0-9]+)~'$b$matches);
        
    $b $matches['key'];
        return (
    $a $b) ? -1;
    }

    usort($images'sort_callback');

    print_r($images);
    /*
    Array
    (
        [0] => img1.jpg
        [1] => img2.jpg
        [2] => img3.jpg
        [3] => img4.jpg
        [4] => img5.jpg
        [5] => img6.jpg
        [6] => img11.jpg
        [7] => img21.jpg
        [8] => img31.jpg
        [9] => img41.jpg
        [10] => img51.jpg
        [11] => img61.jpg
    )
    */
    ?>
    Proper keys now.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Anthony: reinventing the wheel?

    PHP Code:
    $images = array(
        
    'img1.jpg',
        
    'img2.jpg',
        
    'img3.jpg',
        
    'img11.jpg',
        
    'img21.jpg',
        
    'img31.jpg',
        
    'img4.jpg',
        
    'img5.jpg',
        
    'img6.jpg',
        
    'img41.jpg',
        
    'img51.jpg',
        
    'img61.jpg'
    );

    natsort($images);

    var_dump($images);
    /*
    Array
    (
        [0] => img1.jpg
        [1] => img2.jpg
        [2] => img3.jpg
        [3] => img4.jpg
        [4] => img5.jpg
        [5] => img6.jpg
        [6] => img11.jpg
        [7] => img21.jpg
        [8] => img31.jpg
        [9] => img41.jpg
        [10] => img51.jpg
        [11] => img61.jpg
    )
    */ 

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by decowski View Post
    PHP Code:
    $images = array(
        
    'img1.jpg',
        
    'img2.jpg',
        
    'img3.jpg',
        
    'img11.jpg',
        
    'img21.jpg',
        
    'img31.jpg',
        
    'img4.jpg',
        
    'img5.jpg',
        
    'img6.jpg',
        
    'img41.jpg',
        
    'img51.jpg',
        
    'img61.jpg'
    );

    natsort($images);

    var_dump($images);
    /*
    Array
    (
        [0] => img1.jpg
        [1] => img2.jpg
        [2] => img3.jpg
        [3] => img4.jpg
        [4] => img5.jpg
        [5] => img6.jpg
        [6] => img11.jpg
        [7] => img21.jpg
        [8] => img31.jpg
        [9] => img41.jpg
        [10] => img51.jpg
        [11] => img61.jpg
    )
    */ 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    lol glad I got you all thinking before lunch!
    Interestingly the 0 pad DIDN'T work initially however when I renamed all the files with padding to 5 numbers it did work.


    Theres Mondays mystery for you!


    Thanks guys
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •