Hey there. Just picked up some PHP books a few weeks ago and my PHP in a Nutshell book mentioned PHP Builder as a helpful community of smarties.

Prior to that I could dabble in VBScript some.

I'm working on my first project that isn't in the books just to learn, and I've been trying for about 8 hours to figure out how to Sort an array alphabetically by column.

The project:
Home Movie Database

I wanted to create a function for sorting, so I could call that function using a for() loop based on the column I wanted to sort by.

I have 4 columns: Title, Genre, Rating, Media (DVD, Blu Ray, Digital Copy etc.)

I have a tab delimited text file who's lines look like this:

Spider Man 3 	Action - Adventure 	Rated PG 	Blue Ray	
test 	Action - Adventure 	Rated G 	DVD	
test2 	Action - Adventure 	Rated G 	DVD	
The Incredible Hulk 	Action - Adventure 	Rated PG 	Blue Ray	
The Polar Express 	Action - Adventure 	Rated G 	DVD	

At this point I've got probably 200 lines of REMMED out junk code, so let me show you what I do have (And not paste what's NOT working.)

<?php 
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

//(EDITORS NOTE: FAILED FUNCTION...)
/*function compare($w, $x) {
	if ($w[0] == $x) {
		return 0;
	} else if ($w[0] < $x[0]) {
		return -1;
	} else {
		return 1;
	}
}*/
?>

<html>
<head>
	<title>Movie Database - Grid View</title>
</head>
<body>
<h1>Movie Database View</h1>
<?php 
$a = file_get_contents( "$DOCUMENT_ROOT/lessons/movies.txt"); 
$a = str_replace( array( "\n" , "\t" ) , array( "[NEW*LINE]" , "[tAbul*Ator]" ) , $a ); 
//fclose ($a);
echo "<table border=\"1\">\n";
echo "<tr><th bgcolor=\"#CCCCFF\">Movie Title</th>
		  <th bgcolor=\"#CCCCFF\">Genre</th>
		  <th bgcolor=\"#CCCCFF\">Rating</th>
		  <th bgcolor=\"#CCCCFF\">Format</th>
		  <tr>";

//(EDITORS NOTE: Ok, here's a bit more failed code, to give an idea where I was...)
/*for ($layer = 0; $layer < 1; $layer++) {
	echo "Layer $layer<br />";
	for ($row = 0; $row < 999; $row++) {
		for ($column = 0; $column <4; $column++) {
			sort ($a[$layer]); // Extra bit of test code
			echo '|'.$a[$layer][$row][$column];
		}
		echo '|<br />';
	}
}*/

//(EDITORS NOTE: This works to display the array in a grid format in HTMl, but can't be sorted, and I don't want to use foreach)
foreach( explode( "[NEW*LINE]" , $a ) AS $lines ) { 
    echo "<tr>"; 
    foreach( explode( "[tAbul*Ator]" , $lines ) AS $li ) { 
    	echo "<td>"; 
        echo $li ; 
        echo "</td>"; 
    } 

echo "</tr>"; 
} 

echo "</table>";


?>
</body>
</html>

Thanks for your time,
Jack

    I'd suggest looking into using [man]fgetcsv/man to read in the file into an array of arrays (specifying "\t" as the column delimiter).

    $data = array();
    $fh = fopen('path/to/file');
    while($row = fgetcsv($fh, 1000, "\t") !== false)
    {
       $data[] =  $row;
    }
    

    You can then use [man]usort/man to sort that array, specifying which column to sort on in your callback function.

    function sortTitle($a, $b)
    {
       return strnatcmp($a[0], $b[0]);
    }
    
    function sortMedia($a, $b)
    {
       return strnatcmp($a[3], $b[3]);
    }
    
    // sort by media:
    usort($data, 'sortMedia');
    
      Write a Reply...