简体   繁体   English

数组未使用sort()或asort()按字母顺序排序

[英]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. 我试过使用sort()asort()但是输出从不按字母顺序排列,但是顺序确实会改变。 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: 供参考的是未排序的print_r($arr)

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

Here it is using sort() : 在这里,它使用sort()

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

And finally using asort() : 最后使用asort()

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

I don't know which php version you are running, but I would be expecting your script to be emitting: 我不知道您正在运行哪个php版本,但是我希望您的脚本能够发出:

Warning: sort() expects parameter 1 to be array, object given 警告:sort()期望参数1为数组,给定对象

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM