Sorting an Array Using Comparison Functions : usort « Data Structure « PHP






Sorting an Array Using Comparison Functions

 
<?php
  function evenfirst($i, $j)
  {
    $value = 0;

    if($i % 2)$value++;
    if($j % 2)$value--;

    if($value == 0)
      $value = $i < $j;

    return $value;
  }

  $clothes = array( 'hats' => 75, 'coats' => 32, 'shoes' => 102,
                    'gloves' => 15, 'shirts' => 51, 'trousers' => 44);

  usort($clothes, 'evenfirst');

  var_export($clothes);
?>
  
  








Related examples in the same category

1.usort() sorts an array based on your own predefined criteria.