Jump to content


Photo

Array Sort *SOLVED*


  • Please log in to reply
2 replies to this topic

#1 DannyTip

DannyTip
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 20 October 2006 - 06:53 PM

Im using natcasesort() to sort an array of image files.  This works great but i want a method of skipping to the next/previous image file.  But the problem is that when the array is sorted it doesnt reset the internal pointer i.e.

[0] => DSC00346.jpg 
[42] => DSC00347.jpg 
[47] => DSC00351.jpg 
[40] => DSC00353.jpg 
[6] => DSC00354.jpg 
[7] => DSC00355.jpg 
[36] => DSC00356.jpg
...

I know i could use key() to get the pointer for say 'DSC00354.jpg' but is there anyway to get the one for the record after? Or simply reset the pointers so they are still in order.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 20 October 2006 - 07:10 PM

Having sorted the array you can use
foreach ($array as $image) {
        echo $image . '<br />';
}

to process them in order.

If you want the indexes reset to 0, 1, 2
use
$array = array_values($array);

which will give
[pre]
[0] => DSC00346.jpg
[1] => DSC00347.jpg
[2] => DSC00351.jpg
....

[/pre]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 DannyTip

DannyTip
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 20 October 2006 - 07:31 PM

Thankyou

$array = array_values($array);

is exactly what i needed :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users