What is a good way to sort a multi-dimensional array?

Hey SP,

I’m currently writing a transaction/order viewer for my clients and I’m trying to write in different viewing methods of the order. Idealy, I’d like to connect to the database only once to grab the order data and then use PHP to extract the extra and specific to each view kinds of data. For example, our online store has multiple item types, some are books, some are mugs, or bags. So one of the views is by Item Type, another could be the vendor that we purchase the items from, whether or not the items are stock etc.

I have written a MySQL table join that joins multiple tables together, transaction_items, vendors, item_inventory and item_types. It works very well and I do not have any issue with this part. The problem I’m facing now is that I do not any good ways/techniques to go about sorting this rather large Array of order data. Here is an example of what the array looks like.

Array
(
    [0] => Array
        (
            [item_inventory_title] => Book 1
            [item_inventory_price] => 31.3
            [item_inventory_id] => 4010
            [item_inventory_count] => 1
            [item_inventory_barcode] => 8848020939
            [in_order] => 0
            [item_quantity] => 1
            [vendor_name] => Books R'us
            [vendor_id] => 62
            [item_type_name] => Teen Books
            [item_type_id] => 2
            [tax] => 5
        )

    [1] => Array
        (
            [item_inventory_title] => Book 2
            [item_inventory_price] => 21.56
            [item_inventory_id] => 4011
            [item_inventory_count] => 2
            [item_inventory_barcode] =>
            [in_order] => 0
            [item_quantity] => 1
            [vendor_name] => Books R'us
            [vendor_id] => 62
            [item_type_name] => Teen Books
            [item_type_id] => 2
            [tax] => 5
        )

    [2] => Array
        (
            [item_inventory_title] => Book 3
            [item_inventory_price] => 10.96
            [item_inventory_id] => 4008
            [item_inventory_count] => 0
            [item_inventory_barcode] =>
            [in_order] => 0
            [item_quantity] => 1
            [vendor_name] => Books R'us
            [vendor_id] => 18
            [item_type_name] => Teen Books
            [item_type_id] => 2
            [tax] => 5
        )

    [3] => Array
        (
            [item_inventory_title] => Book 4
            [item_inventory_price] => 24.95
            [item_inventory_id] => 3999
            [item_inventory_count] => 0
            [item_inventory_barcode] =>
            [in_order] => 0
            [item_quantity] => 1
            [vendor_name] => Books R'us
            [vendor_id] => 18
            [item_type_name] => Teen Books
            [item_type_id] => 2
            [tax] => 5
        )

)

As you can see, the data is an array of arrays and the sorting indexes would be from data inside of one of the internal arrays. I have no clue how to go about sorting all of the arrays via an index inside one of these internal arrays. I know I can get around this issue by just writing different MYSQL sort code for each type of View mode I’m writing, but I’d really like to just grab all of the data and sort it with PHP. It would make coding these multiple views easier and would prevent code over-lap.

Any tips or suggestions in the right direction are greatly appreciated. Thank you.

Sounds like you’re looking for [fphp]array_multisort[/fphp]. Check out example #3 on that page, that one does what you want to do :slight_smile:

Thank you. I’ll check that out.