Good day to you, I have an multi-dimensional array and I need to sort it by date and then height. My question is how would I build my code to do such a function ? Here is how I have built my array : <?php $imgdir= $_GET['folder']; $imgdir.="/"; function dircont($imgdir) { $dir_array = array(); if (false !== ($dir = opendir($imgdir))) { while (false !== ($file = readdir($dir))) { if ($file != '.' && $file != '..') { $fInfo = array(); $fInfo[] = $file; if (($imageInfo = @getImageSize($imgdir."".$file)) !== false) { $fInfo['width'] = $imageInfo[0]; $fInfo['height'] = $imageInfo[1]; } if (($stat = stat($imgdir."".$file)) !== false) { $fInfo['mtime'] = $stat['mtime']; // Lol, forgot to put the size in :P $fInfo['size'] = $stat['size']; } $dir_array[] = $fInfo; } } return $dir_array; } else { return false; } } // To print.. echo "<pre>"; print_r(dircont($imgdir)); echo "</pre>"; ?> PHP:
Try adding a first index which is a concatenation of the date time. For example $fInfo[date.'--'.time] and sort by that. You must make sure that the key is unique and that the length of each key is the same (pad with 0's if needed).
Don't have time to research all the functions and code something specific, but I whipped this up in a couple mins: <pre><?php $array_to_sort = array('1400'=> array(2=>"Yes", 1=>"No"), '200'=> array(32=>"Maybe",33=>"Sure"), '100'=> array(100=>"Good",50=>array("Bad","Really Bad"),75=>"Ugly"), ); $sorted_array = array(); foreach ($array_to_sort as $first_dimension_index=>$second_dimension) { foreach($second_dimension as $second_dimension_index=>$dimensional_value) { $sorted_array[str_pad($first_dimension_index,5,'0',STR_PAD_LEFT).'--'.str_pad($second_dimension_index,5,'0',STR_PAD_LEFT)] = $dimensional_value; } } ksort($sorted_array); print_r($sorted_array); ?> PHP: That should give you enough of an idea to see what I mean.
where about should i put such a code in mine ? I have tried many place changing foreach ($dir_array as $first_dimension_index=>$second_dimension) { <?php $imgdir= $_GET['folder']; $imgdir.="/"; function dircont($imgdir) { $dir_array = array(); if (false !== ($dir = opendir($imgdir))) { while (false !== ($file = readdir($dir))) { if ($file != '.' && $file != '..') { $fInfo = array(); $fInfo[] = $file; if (($imageInfo = @getImageSize($imgdir."".$file)) !== false) { $fInfo['width'] = $imageInfo[0]; $fInfo['height'] = $imageInfo[1]; } if (($stat = stat($imgdir."".$file)) !== false) { $fInfo['mtime'] = $stat['mtime']; // Lol, forgot to put the size in :P $fInfo['size'] = $stat['size']; } $sorted_array = array(); foreach ($dir_array as $first_dimension_index=>$second_dimension) { foreach($second_dimension as $second_dimension_index=>$dimensional_value) { $sorted_array[str_pad($first_dimension_index,5,'0',STR_PAD_LEFT).'--'.str_pad($second_dimension_index,5,'0',STR_PAD_LEFT)] = $dimensional_value; } } $dir_array[] = $fInfo; } } return $dir_array; } } ksort($sorted_array); print_r($sorted_array); ?> PHP:
Oh, man, you missed the point. Read the code for the concept. I wasn't giving you something you could use directly. You'd have to study it first and then apply the concept. In forums, I try to teach. When I'm paid, I just do the work. Now, though, I'm tired, so outta here for tonight (and tomorrow, a holiday).
Enjoy, thanks for your time, i'm working on it, I didn't thought so, but I thought that part fitted there , with my slow learning brain . I'm learning, i'm taking themost of my time working on project like that, I'll know !!!!!!!!!! Take good care and thanks !