Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 14th, 2003, 09:32 AM
Registered User
 
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sorting an array

Hello people. `

As a rookie, I'm having problems with Asort(). In the script below I open a directory, read all the html files in this directory, then grabbing line 5 out of each one of them to create a link to them. the problem is I'm having a hard time sorting the links alphabeticaly.

<?php
$default_dir = "./";
function traverse_dir($dir) {
   chdir($dir);
   if(!($dp = opendir($dir))) die("Can't open $dir.");
   while($file = readdir($dp)) {
   if(substr($file, -5)== ".html"){
   $filelines = file($file);
   $links = $filelines[4];
   $links = ereg_replace("<TITLE>", "", $links);
   $links = ereg_replace("</TITLE>", "", $links);
   echo "<a href=\"".$file."\">".$links."</a><BR>";
    }}
   closedir($dp);
}
?>

how would someone sort $link variable in this case? I tried alot of things, and something seems to escapes me because I cant seem to get it.

 
Old October 14th, 2003, 01:16 PM
Registered User
 
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks guys but I got it: (well maybe I cheated... but like I said I'm a rookie)

<?php
$default_dir = "./";
function traverse_dir($dir) {
$counter = 0;
$ordered_links = array();
   chdir($dir);
   if(!($dp = opendir($dir))) die("Can't open $dir.");
   while($file = readdir($dp)) {
   if(substr($file, -5)== ".html"){
   $filelines = file($file);
   $links = $filelines[4];
   $links = ereg_replace("<TITLE>", "", $links);
   $links = ereg_replace("</TITLE>", "", $links);
   $links = ereg_replace("\\\\n", "", $links);
   $ordered_links[$counter] = $links."@".$file;
   $counter++;
   //echo "<a href=\"".$file."\">".$links."</a><BR>";
    }}
   closedir($dp);
   asort($ordered_links);
   foreach ($ordered_links as $subject){
   $file = explode("@", $subject);
   echo "<a href=\"".$file[1]."\">".$file[0]."</a><BR>";
   }
}
?>






Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting Array in C++ code_lover C++ Programming 4 October 16th, 2008 01:43 AM
Sorting 2 dimensional array rgalehouse Javascript How-To 2 December 2nd, 2006 10:46 AM
error when sorting an Array of Array nancy VBScript 2 February 17th, 2005 12:57 PM
sorting two dimensional array erin VBScript 0 February 10th, 2004 05:55 PM
Array Sorting Dinesh22 VB.NET 2002/2003 Basics 2 February 3rd, 2004 11:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.