Location: PHPKode > scripts > Array Sorter > array_example.php
<?php
/**
* Example of sorting multidimensional array
*
*/

//test data
$ar = array();
$ar[0]['name'] = 'Terry';
$ar[0]['surname'] = 'Cook';
$ar[0]['addr']['street'] = 'ABBAY ROAD';
$ar[0]['addr']['no'] = '10';
$ar[0]['salary'] = 25000;

$ar[1]['name'] = 'Anna';
$ar[1]['surname'] = 'Smith';
$ar[1]['addr']['street'] = 'STREATHAM PLACE';
$ar[1]['addr']['no'] = '1';
$ar[1]['salary'] = 15000;

$ar[2]['name'] = 'John';
$ar[2]['surname'] = 'Doe';
$ar[2]['addr']['street'] = 'GAUNT STREET';
$ar[2]['addr']['no'] = '101';
$ar[2]['salary'] = 30000;

require('sorter.php');

//initialize sorter
$oSorter = new ArraySorter();
//set data to sort
$oSorter->setArray($ar);

echo "<pre>";
//sort by addr street ascending
print_r($oSorter->sort('addr.street', ArraySorter::DIRECTION_ASC));

echo '<hr />';
//sort by salary descending
print_r($oSorter->sort('salary', ArraySorter::DIRECTION_DESC));
Return current item: Array Sorter