bertrc I want to compare the array with this function two dimensions
usort()
does that for you. Whether what you wrote would "work" may depend on your answer to Weedpacket's question. Right now you'd be sorting on the 2nd element of each sub-array (since they are zero-indexed). If possible, you might want to turn that into an associative array, so that you can be explicit in your code as to which field(s) to sort by. E.g.:
$products1 = array(
array('id' => 'TIR', 'name' => 'Tires', 'value' => 100),
array('id' => 'OIL', 'name' => 'Oil', 'value' => 10),
array('id' => 'SPK', 'name' => 'Spark Plugs', 'value' => 4)
);
function compare_name($x, $y) {
// just because I can, not that it's necessarily "better"
// than using if/elseif/else -- just shorter:
return(min(1, max(-1, ($x['name'] <=> $y['name']))));
}
function compare_value($x, $y) {
return(min(1, max(-1, ($x['value'] <=> $y['value']))));
}
// sort by the name
usort($products1,'compare_name');
print_r($products1);
// sort by the value
usort($products1, 'compare_value');
print_r($products1);