Sort multidimensional array using associative indexing

Sort multidimensional array using associative indexing

Post by Pakk » Sun, 27 Apr 2008 04:32:43

Trying to get to grips with the various sort functions here.

This is my array
$items = array( array( 'Name' => 'BLB', 'Desc' =>'Bulbs', 'Cost' =>
1 ),
array( 'Name' => 'CBL', 'Desc' =>'Cables', 'Cost'
=>3 ),
array( 'Name' => 'PLG', 'Desc' =>'Plugs', 'Cost'
=>4 ) );

function compare($x, $y)

if ( $x['Desc'] == $y['Desc'] )
return 0;
else if ( $x['Desc'] < $y['Desc'] )
return -1;
return 1;

Since this is indexed associatively I though I should use uksort to
sort this but I am finding that usort suffices.

so uksort$items, 'compare'); doesn't work

but usort($items,'compare'); does

Any ideas?


1. Indexed array of associative arrays quandary - how do I sort and sum?

2. Indexed array of associative arrays quandary - how do I sort and sum?

I am stumped by this one.

I have an indexed array of associative arrays. The name of the indexed
array is $unique.
I want to go through the indexed array and add up the total values for
one key => value pair (quantity =>) depending on the value of another
key => value pair (name=>).

Associative array structure:
array( 'name' => $item_name,
'quantity' => $item_quantity,
'unit' => $item_unit)

Typical array values:
array( 'name' => 'Recycling - General',
'quantity' => '45',
'unit' => 'pounds')

array( 'name' => 'Trash by Weight',
'quantity' => '12',
'unit' => pounds)

array( 'name' => 'Trash by Volume',
'quantity' => '1',
'unit' => 'count')

array( 'name' => 'Recycling - General',
'quantity' => '12',
'unit' => 'pounds')

So for each 'name' that is the same I want to add the 'quantity' to a
running total and in the end have a list of name and quantity totals.

Any help would be greatly appreciated, I am stumped.

3. Difference between accessing arrays and associative arrays using a int index

4. associative array of associative arrays

5. Associative arrays of associative arrays

6. Associative Array with ID as Key and other associative array as value?

7. associative array where index uses [ ]

8. Associative with MultiDimensional arrays

9. Multidimensional associative array

10. Multidimensional associative arrays in awk ?

11. multidimensional associative array

12. Associative multidimensional arrays

13. Multidimensional Associative Arrays..

14. PL/SQL Associative Arrays - Index Not In Array

15. Using ~variable as index to multidimensional arrays in SystemVerilog