CodeIgniter Forums
Problems with sorting arrays returned by get_filenames() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problems with sorting arrays returned by get_filenames() (/showthread.php?tid=6283)



Problems with sorting arrays returned by get_filenames() - El Forum - 02-21-2008

[eluser]swissbastian[/eluser]
Hi there

I got a problem with sorting arrays. The array is returned by the function get_filenames(). It looks like this:

Array ( [0] => 48.jpg [1] => 103.jpg [2] => 1.jpg [3] => 47.jpg [4] => 104.jpg [5] => 2.jpg [6] => 49.jpg [7] => 105.jpg [8] => 3.jpg [9] => 50.jpg [10] => 106.jpg [11] => 4.jpg [12] => 51.jpg [13] => 107.jpg [14] => 5.jpg [15] => 57.jpg [16] => 108.jpg )

Well, I don't know why the function puts the files in this order. It shouldn't be a problem when I sort the array. I've tried now several sorting functions (like sort, natsort, asort…) but I never worked.

Here's some code from the affected model:
Code:
$dateiliste_unsortiert = get_filenames('dateien/fotos/' . $album . '/');
        $dateiliste = natsort($dateiliste_unsortiert);
        
        foreach($dateiliste as $zeile) {
        
            $dateien[$zeile] = $zeile;
        
        }
        
           return $dateien;

Any idea what I'm doing wrong?


Problems with sorting arrays returned by get_filenames() - El Forum - 02-21-2008

[eluser]xwero[/eluser]
The natsort function would be the way to go. What is the array you get after it's been manipulated by natsort?


Problems with sorting arrays returned by get_filenames() - El Forum - 02-21-2008

[eluser]swissbastian[/eluser]
Hm, it's really scary.

Okay, this is the model function which is called by the controller:
Quote:function get_fotovorschau($album)
{
$dateiliste = get_filenames('dateien/fotos/' . $album . '/');

foreach($dateiliste as $zeile) {

$dateien[$zeile] = $zeile;

}

return $dateien;
}

The controller itselfs passes the array to the view file. There I put the following:
Quote:print_r($dateien);
$sortiert = natsort($dateien);
print_r($sortiert);

And that's what the browser puts out for the unsorted array:
Quote:Array ( [1.jpg] => 1.jpg [2.jpg] => 2.jpg [3.jpg] => 3.jpg [4.jpg] => 4.jpg [5.jpg] => 5.jpg [6.jpg] => 6.jpg [7.jpg] => 7.jpg [8.jpg] => 8.jpg [9.jpg] => 9.jpg [10.jpg] => 10.jpg )

All fine. Okay, in this example, the array values are already in the right order. But consider the case it wouldn't be so, the natsort function should return a sorted array, I guess.

But it returns:
Code:
1

No array anymore… hmmm…


Problems with sorting arrays returned by get_filenames() - El Forum - 02-21-2008

[eluser]xwero[/eluser]
Oh of course the sort functions manipulate the array by reference. This means the array is manipulated directly. The output of the function just tell you if the function did its work or not.
Code:
print_r($dateien);
natsort($dateien);
print_r($dateien);



Problems with sorting arrays returned by get_filenames() - El Forum - 02-21-2008

[eluser]swissbastian[/eluser]
Okay, I understand… a little bit. :-)

So what is the way to do it?

I've got this variable with the unsorted array returned by the model function. How can I get the sorted array into a useable variable to go on with?


Problems with sorting arrays returned by get_filenames() - El Forum - 02-21-2008

[eluser]xwero[/eluser]
I don't understand why you would want to use the filename as key and as value?

The shortest code would be
Code:
// controller
function album()
{
    if(!$this->uri->segment(3))
    {
       redirect('albums');
    }
    else
    {
         $dateiliste = get_filenames(’dateien/fotos/’ . $this->uri->segment(3) . ‘/’);
         natsort($dateiliste);
         $this->load->view('album',array('fotos'=>$dateiliste,'urlfotodir'=>'http://site.com/fotos/'));
    }
}
// view
<ul>
&lt;?php foreach($fotos as $foto){ ?&gt;
<li><img src="&lt;?php echo $urlfotodir.$foto; ?&gt;"></li>
&lt;?php } ?&gt;
</ul>



Problems with sorting arrays returned by get_filenames() - El Forum - 02-21-2008

[eluser]swissbastian[/eluser]
Ahh, I got it!

Thanks for your help.