Solved

Sorting array in PHP

Posted on 2014-12-15
5
134 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

757 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

22 Experts available now in Live!

Get 1:1 Help Now