Solved

Sorting array in PHP

Posted on 2014-12-15
5
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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 …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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 look for a specific file type in a local or remote server directory using PHP.

742 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