Solved

Sorting array in PHP

Posted on 2014-12-15
5
140 Views
Last Modified: 2014-12-16
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
Comment
Question by:edavo
  • 2
  • 2
5 Comments
 

Author Comment

by:edavo
ID: 40501968
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
 
LVL 7

Accepted Solution

by:
Phil Davidson earned 500 total points
ID: 40501987
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40502372
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
 

Author Comment

by:edavo
ID: 40502904
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40503764
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now