通过PHP中的deply嵌套数组对多维和关联数组进行排序

时间:2021-06-24 21:31:47

I have a multidimensional array that I fetch from a database and it's structured like this:

我有一个多维数组,我从数据库中获取它的结构如下:

array(SESSION
      array(items
            array(DatabaseID
                  (ItemName, ItemCategory, Children, 
                                                    array(Id1, Id2, Id3...)         
                                                       etc..))))

I am not sure that this is how you write it since I'm a beginner but I hope you get the struture. If I want to acess the ItemCategory of a certain item, I save the item's DatabaseID in $DatabaseID and write:

我不确定这是你怎么写的,因为我是初学者,但我希望你能得到结论。如果我想访问某个项目的ItemCategory,我将项目的DatabaseID保存在$ DatabaseID中并写入:

$_SESSION['items'][$DatabaseID]['ItemCategory'];

If I want an array with the items children's DatabaseID I write:

如果我想要一个带有子项的DatabaseID的数组我写:

$_SESSION['items'][$DatabaseID]['Children'];

Now, I want to sort this array. I have looked around but I don't understand how to sort it after exactly what I want. I would like to sort the whole

现在,我想对这个数组进行排序。我环顾四周,但我不明白如何按照我想要的方式对其进行排序。我想整理一下

$_SESSION['items']

according to the ItemName instead of the DatabaseID. Is this at all possible? I mean, the ItemName is stored for each DatabaseID...

根据ItemName而不是DatabaseID。这是可能吗?我的意思是,为每个DatabaseID存储ItemName ...

I want to use this in order print all the Items sorted by their name instead of their DatabaseID.

我想使用它来打印按名称而不是DatabaseID排序的所有项目。

Edit

I have tried

我努力了

array_multisort($_SESSION['items'], $_SESSION['items']['DatabaseID']['ItemName']);

but the problem is that

但问题是

 $_SESSION['items']['DatabaseID']['ItemName']

is not an array.

不是一个数组。

1 个解决方案

#1


0  

An easier and faster solutiom is to use the SQL ORDER BY clause.

更简单快捷的解决方案是使用SQL ORDER BY子句。

The syntax is:

语法是:

SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;

SELECT column1,column2,... FROM table_name ORDER BY column1,column2,... ASC | DESC;

You should add this clause onto your existing SQL statement.

您应该将此子句添加到现有SQL语句中。

#1


0  

An easier and faster solutiom is to use the SQL ORDER BY clause.

更简单快捷的解决方案是使用SQL ORDER BY子句。

The syntax is:

语法是:

SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;

SELECT column1,column2,... FROM table_name ORDER BY column1,column2,... ASC | DESC;

You should add this clause onto your existing SQL statement.

您应该将此子句添加到现有SQL语句中。