1. ## 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.

2. Natural sorting problem. Padding with 0's should help, however. You must be doing something wrong.

You could also use natsort().

3. Yep, 01 02 03 04 05 06 07 08 09 10 11... should work.

4. 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...

5. 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.

6. @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. Originally Posted by decowski
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
)
*/

8. 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

#### Posting Permissions

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