PHP Classes

File: array_example.php

Recommend this page to a friend!
  Classes of Przemek Berezowski   Array Sorter   array_example.php   Download  
File: array_example.php
Role: Example script
Content type: text/plain
Description: Usage example with multidimensional array
Class: Array Sorter
Sort multidimensional arrays
Author: By
Last change:
Date: 13 years ago
Size: 885 bytes
 

Contents

Class file image Download
<?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));