0

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!

1
  • 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. Commented Sep 1, 2013 at 12:01

2 Answers 2

1

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
)
Sign up to request clarification or add additional context in comments.

1 Comment

you are a godsend! worked perfectly.. I understand what you did also! Thank you for teaching me something new today :)
0

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]");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.