Login   Register  
PHP Classes
elePHPant
Icontem

File: array_of_objects_example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Przemek Berezowski  >  Array Sorter  >  array_of_objects_example.php  >  Download  
File: array_of_objects_example.php
Role: Example script
Content type: text/plain
Description: Usage example with array of objects
Class: Array Sorter
Sort multidimensional arrays
Author: By
Last change:
Date: 2011-03-04 02:57
Size: 967 bytes
 

Contents

Class file image Download
<?php
/**
 * Example of sorting array of objects
 * 
 */

//test data
$ar = array();

$o = new stdClass();
$o->name 'Terry';
$o->surname 'Cook';
$o->addr = new stdClass();
$o->addr->street 'ABBAY ROAD';
$o->addr->no '10';
$o->salary 25000;
$ar[0] = $o;

$o = new stdClass();
$o->name 'Anna';
$o->surname 'Smith';
$o->addr = new stdClass();
$o->addr->street 'STREATHAM PLACE';
$o->addr->no '1';
$o->salary 15000;
$ar[1] = $o;

$o = new stdClass();
$o->name 'John';
$o->surname 'Doe';
$o->addr = new stdClass();
$o->addr->street 'GAUNT STREET';
$o->addr->no '101';
$o->salary 30000;
$ar[2] = $o;


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));