Jesse Elser
Jesse Elser

Reputation: 976

Array not sorting alphabetically with sort() or asort()

I have the following array that is used to create a list of buttons. These buttons are not in alphabetical order, but I am aiming to put them alphabetically. Here is the array:

<?xml version="1.0" encoding="utf-8"?>   
<categories>
    <category>Landscape</category>
    <category>Wildlife</category>
    <category>Action/Sports</category>
    <category>Portrait</category>
    <category>Architectural</category>
    <category>Wedding</category>
    <category>Events</category>
    <category>Fashion</category>
    <category>Macro</category>
    <category>Family</category>
    <category>Baby</category>
    <category>Abstract</category>
    <category>Bodyscape</category>
    <category>Forced Perspective</category>
    <category>Modeling</category>
</categories>

Now I know how to output an array into a list of buttons and I know how to sort arrays(or so I thought). I have the code below for my output. I have tried using sort() and asort() but the output is never alphabetical, but the order does change. What might I be doing wrong?

$gallery_catdata = new SimpleXMLElement('xml/gallery_cat.xml', 0, true);
$arr=array();
foreach($gallery_catdata->category as $category)
{
    $arr[]=$category;
}
//print_r($arr);
/* uncomment the above line to debug */
asort($arr);
//print_r($arr);
/* uncomment the above line to debug */
foreach($arr as $categories) {       
    $category = str_replace(' ', '-', $categories);
    $category = strtolower($category);
                            echo '<button data-filter=".portfolio-filter-'.$category.'">'.$categories.'</button>';
} 

For reference here is the print_r($arr) unsorted:

Array ( [0] => SimpleXMLElement Object ( )
[1] => SimpleXMLElement Object ( )
[2] => SimpleXMLElement Object ( )
[3] => SimpleXMLElement Object ( )
[4] => SimpleXMLElement Object ( )
[5] => SimpleXMLElement Object ( )
[6] => SimpleXMLElement Object ( )
[7] => SimpleXMLElement Object ( )
[8] => SimpleXMLElement Object ( )
[9] => SimpleXMLElement Object ( )
[10] => SimpleXMLElement Object ( )
[11] => SimpleXMLElement Object ( )
[12] => SimpleXMLElement Object ( )
[13] => SimpleXMLElement Object ( )
[14] => SimpleXMLElement Object ( )
)

Here it is using sort():

Array ( [0] => SimpleXMLElement Object ( )
[1] => SimpleXMLElement Object ( )
[2] => SimpleXMLElement Object ( )
[3] => SimpleXMLElement Object ( )
[4] => SimpleXMLElement Object ( )
[5] => SimpleXMLElement Object ( )
[6] => SimpleXMLElement Object ( )
[7] => SimpleXMLElement Object ( )
[8] => SimpleXMLElement Object ( )
[9] => SimpleXMLElement Object ( )
[10] => SimpleXMLElement Object ( )
[11] => SimpleXMLElement Object ( )
[12] => SimpleXMLElement Object ( )
[13] => SimpleXMLElement Object ( )
[14] => SimpleXMLElement Object ( )
)

And finally using asort():

Array ( [0] => SimpleXMLElement Object ( )
[1] => SimpleXMLElement Object ( )
[11] => SimpleXMLElement Object ( )
[2] => SimpleXMLElement Object ( )
[9] => SimpleXMLElement Object ( )
[3] => SimpleXMLElement Object ( )
[13] => SimpleXMLElement Object ( )
[4] => SimpleXMLElement Object ( )
[8] => SimpleXMLElement Object ( )
[5] => SimpleXMLElement Object ( )
[10] => SimpleXMLElement Object ( )
[6] => SimpleXMLElement Object ( )
[12] => SimpleXMLElement Object ( )
[7] => SimpleXMLElement Object ( )
[14] => SimpleXMLElement Object ( )
)`

Upvotes: 0

Views: 188

Answers (1)

mickmackusa
mickmackusa

Reputation: 48100

I don't know which php version you are running, but I would be expecting your script to be emitting:

Warning: sort() expects parameter 1 to be array, object given

Just convert your data structure from an object to an array via explicit casting.

Code: (Demo)

$xml = <<<XML
<categories>
    <category>Landscape</category>
    <category>Wildlife</category>
    <category>Action/Sports</category>
    <category>Portrait</category>
    <category>Architectural</category>
    <category>Wedding</category>
    <category>Events</category>
    <category>Fashion</category>
    <category>Macro</category>
    <category>Family</category>
    <category>Baby</category>
    <category>Abstract</category>
    <category>Bodyscape</category>
    <category>Forced Perspective</category>
    <category>Modeling</category>
</categories>
XML;

$categories = (array)simplexml_load_string($xml, 'SimpleXMLElement')->category;

sort($categories);

foreach ($categories as $category) {
    echo $category , "\n";
}

Upvotes: 2

Related Questions