Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

Sorting array in PHP

This is my code and it works - BUT - I want the array list to be listing in alphabetical order>

$sList = $sList2 = '';
foreach ($aFiles as $sSingleFile) {
    $aTags = $oReader->getTagsInfo($sSingleFile); // obtaining ID3 tags info
    $sList .= '<tr><td>'.$aTags['Title'].'</td><td>'.$aTags['Album'].'</td><td>'.$aTags['Author'].'</td>
                    <td>'.$aTags['AlbumAuthor'].'</td><td>'.$aTags['Track'].'</td><td>'.$aTags['Year'].'</td>
                    <td>'.$aTags['Desc'].'</td>
                    <td>'.$aTags['Genre'].'</td></tr>';
//   DELETED LENGTH + LYRIC FROM THIOS LINE <td>'.$aTags['Lenght'].'</td><td>'.$aTags['Lyric'].'</td><td>'.$aTags['Desc'].'</td>

    $sList2 .= '<tr><td>'.$aTags['Title'].'</td><td>'.$aTags['Encoded'].'</td><td>'.$aTags['Copyright'].'</td>
                    <td>'.$aTags['Publisher'].'</td><td>'.$aTags['OriginalArtist'].'</td><td>'.$aTags['URL'].'</td>
                    <td>'.$aTags['Comments'].'</td><td>'.$aTags['Composer'].'</td></tr>';
}
// main output
// echo strtr(file_get_contents('main_page.html'), array('__list__' => $sList, '__list2__' => $sList2));

echo '<table border="1">';
// HERE IS WHER ETHE LIST IS DISPLAYED
// HOW CAN I GET THIS TO DISPLAY IN ALPHA ORDER?
echo $sList;
echo '</table>';

Open in new window

0
edavo
Asked:
edavo
  • 2
  • 2
1 Solution
 
edavoAuthor Commented:
Here is a revised version:

echo '<table class="song_list">';

// HERE is where I need to sort the list?????

foreach ($aFiles as $sSingleFile) {
    $aTags = $oReader->getTagsInfo($sSingleFile); // obtaining ID3 tags info
    echo '<tr>';
	echo '<td>'.$aTags['Title'].'</td>';
	echo '<td>'.$aTags['Album'].'</td>';
	echo '<td>'.$aTags['Author'].'</td>';
    echo '<td>'.$aTags['AlbumAuthor'].'</td>';
	echo '<td>'.$aTags['Track'].'</td>';
	echo '<td>'.$aTags['Year'].'</td>';
	// echo '<td>'.$aTags['Length'].'</td>';
    echo '<td>'.$aTags['Desc'].'</td>';
    echo '<td>'.$aTags['Genre'].'</td>';
	// echo '<td>'.$aTags['Lyrics'].'</td>';
	// echo '<td>'.$aTags['Copyright'].'</td>';
	// echo '<td>'.$aTags['URL'].'</td>';
	// echo '<td>'.$aTags['Publisher'].'</td>';
	// echo '<td>'.$aTags['Comments'].'</td>';
	echo '</tr>';
}
echo '</table>';

Open in new window

0
 
Phil DavidsonCommented:
It looks like you want to sort at the top (based on the revised post).

Alphabetized by title, ID3 tags or albumAuthor or something else?

Sort is a reserved word in PHP.

<?php
sort($aFiles);
foreach ($aFiles as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}
?>

Open in new window


The above code was my version (and modification) of what I found here for alphabetizing: http://php.net/manual/en/function.sort.php

I'm not a PHP expert.  Can you see if the above works?  You may have to modify $aFiles to be something else.  The link above may help.
0
 
Ray PaseurCommented:
Like most things in PHP, a search of the PHP.net web site will lead you in the right direction.  For example, if you search for "sort" this is the first link returned:

http://php.net/manual/en/function.sort.php

PHP has several sorting algorithms:  

http://php.net/manual/en/array.sorting.php
0
 
edavoAuthor Commented:
Thank you - Yes, you are correct but when newbies get different angles and solutions that are more pinpointed, it eliminates a lot of confusion. I often frequent php.net. It is an invaluable source. Often we pull our hair out over the simplest of newbie and syntax errors. Silly beginner mistakes. But you are correct.
0
 
Ray PaseurCommented:
Understood -- learning any new language is a real commitment of time and effort.  Personally, I could not work with PHP unless I had a window open to the online manual!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now