Solved

Sorting array in PHP

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

624 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