0

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:

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

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

$_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...

I want to use this in order print all the Items sorted by their name instead of their 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.

Cœur
  • 32,421
  • 21
  • 173
  • 232
e.klara.k
  • 339
  • 1
  • 3
  • 16
  • 1
    possible duplicate of [Reference: all basic ways to sort arrays and data in PHP](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php) – Epodax Sep 30 '15 at 08:58
  • Unfortunally not... @Epodax – e.klara.k Sep 30 '15 at 09:19
  • In the last two coding examples, you are purposely stating "DatabaseID", but when you actually try to access it in code you replace with the variable `$DatabaseID` right? – Brandon White Sep 30 '15 at 11:41

1 Answers1

0

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

The syntax is:

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

You should add this clause onto your existing SQL statement.

djacobsdev
  • 71
  • 6