Hello, can anyone help me with a code that will sort arrays around within a multi-dimension array.

I have an array, with a whole buch of arrays inside, and I want to sort those arrays inside around based on a key common to all arrays.

Problem is I have a big array with the results of a few mysql queries inside it, now I want to sort those around like as if it were just one big query.

    Well - I solved my problem now, but I will post here what it was if it helps other people

    Structure of array:

    Array (
    [0] => Array ( [cust_id] => 307 [inv_date] => 1020853454 [name] => Fred )
    [1] => Array ( [cust_id] => 1 [inv_date] => 1020851903 [name] => Bob )
    [2] => Array ( [cust_id] => 358 [inv_date] => 1029325324 [name] => Willy )
    [3] => Array ( [cust_id] => 18 [inv_date] => 1025022376 [name] => Tom )
    )

    My array had heaps more keys in it, but this is a simplified version. I wanted to sort all the array around based on a user-defined key.

    eg: if the user wants it sorted by the name, the array after the sort should be:

    Array (
    [0] => Array ( [cust_id] => 1 [inv_date] => 1020853454 [name] => Bob )
    [1] => Array ( [cust_id] => 307 [inv_date] => 1020851903 [name] => Fred )
    [2] => Array ( [cust_id] => 18 [inv_date] => 1025022376 [name] => Tom )
    [3] => Array ( [cust_id] => 358 [inv_date] => 1029325324 [name] => Willy )

    )

    the code I ended up using is:

    	function sorter ($a, $b) {
    		global $sort_by;
    		global $order_by;
    		$a_str = $a[$sort_by] . $a['inv_date'] . $a['name'];
    		$b_str = $b[$sort_by] . $b['inv_date'] . $b['name'];
    		if ($order_by) {
    			return strcasecmp($b_str, $a_str);
    		} else {
    			return strcasecmp($a_str, $b_str);
    		}
    	}
    	uasort ($match_inv, "sorter");
    
      Write a Reply...