Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a folder containing php files from 1-25 each titled 1.php 2.php etc. I am wanting to echo the contents of each file in an array on a page using php in a descending order.

The problem I am having is that it is currently outputting in reverse order, when it gets to 10, it will then put 11 next to 1. This is not the logical system how a human would interpret.

I have been looking at this: http://php.net/manual/en/array.sorting.php and I have tried arsort, krsort, natcasesort and rsort with little success to logically ordering my array after ten in a descending order starting at highest number first. The full code is as follows:

$articles = glob("$_SERVER[DOCUMENT_ROOT]/assets/news/overview/*.php");

if(count($articles)) {
  rsort($articles);

foreach($articles as $article) {
    $article = basename($article);


include("$_SERVER[DOCUMENT_ROOT]/assets/news/overview/$article");

      }

    }

else {

echo "Sorry, no articles.";

}

Can anyone help me out with finishing this php script off? Thank you!

share|improve this question
    
Thank god for us, computers do what they are programmed to do and not just what we want them to do... Articles are being sorted as strings, but you want them sorted as integers... Going to be difficult. Either extract only the number portion of your filenames or use '01.php', '02.php' as filenames. –  Marty McVry Sep 1 '13 at 12:01

2 Answers 2

I would use something like this:

if(count($articles)) {
  natsort($articles);
  $articles = array_reverse($articles);
}

Example:

$a = array(
    "1.php",
    "12.php",
    "10.php",
    "3.php",
    "4.php",
    "5.php",
    "11.php",
    "2.php",
    "21.php",
    "13.php",                                       
    "22.php"); 
natsort($a);           
$a = array_reverse($a);

print_r($a);

with result:

Array (                                      
  [0] => 22.php
  [1] => 21.php
  [2] => 13.php
  [3] => 12.php     
  [4] => 11.php       
  [5] => 10.php 
  [6] => 5.php    
  [7] => 4.php 
  [8] => 3.php 
  [9] => 2.php
  [10] => 1.php
)
share|improve this answer
    
you are a godsend! worked perfectly.. I understand what you did also! Thank you for teaching me something new today :) –  Luke C Sep 1 '13 at 12:07

It is sorting the elements by filename, which is a string, not a number. You should convert the filename without extension to integers and add them to a new array. Then reverse sort the array by the indexes.

$sortedArticles = array();
foreach($articles as $article) {
    $sortedArticles[(int)basename($article)] = $article;
}

uksort($sortedArticles, function($a, $b) {
    return $b - $a
});

for($i = 1; $i <= count($articles); $i++) {
    include("$_SERVER[DOCUMENT_ROOT]/assets/news/overview/$sortedArticles[$i]");
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.