PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of Omar Ramos   Complete Array Object   index.php   Download  
File: index.php
Role: Example script
Content type: text/plain
Description: This file contains some example usages of some of the new methods.
Class: Complete Array Object
Manipulate arrays of strict types
Author: By
Last change:
Date: 15 years ago
Size: 2,546 bytes
 

Contents

Class file image Download
<?php

include_once('CompleteArrayObject.php');
$example_numeric_array = range(1, 1000);
$intCOA = new CompleteArrayObject($example_numeric_array, 'int');

// Should throw an exception
try {
   
$intCOA[] = 1001; // Valid
    // Uncomment to see exception
    //$intCOA[] = 'some_string'; // Invalid
} catch (InvalidArgumentException $e) {
    echo
$e;
}

echo
"intCOA sum(): " . $intCOA->sum() . "<br />";
echo
"intCOA max(): " . $intCOA->max() . "<br />";
echo
"intCOA min(): " . $intCOA->min() . "<br />";
echo
"intCOA avg(): " . $intCOA->avg() . "<br />";
$intCOA[] = 777;
$intCOA[] = 777;
$intCOA[] = 779;
$intCOA[] = 779;
$intCOA[] = 779;
// The mode will returns a CompleteArrayObject of CompleteArrayObjects
// if there are multiple mode values. In the case of a only a single mode
// the mode value will be returned.
echo "intCOA mode(): " . $intCOA->mode() . "<br />";
echo
"intCOA range(): " . $intCOA->range() . "<br />";
echo
"intCOA product(): " . $intCOA->product() . "<br />";


/*** a simple array ***/
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');

$animalCOA = new CompleteArrayObject($array); // Creates new untyped COA instance

$animalCOA->arsort();
echo
"After arsort():<br />";
echo
$animalCOA;

$animalCOA->asort();
echo
"After asort():<br />";
echo
$animalCOA;

$animalCOA->krsort();
echo
"After krsort():<br />";
echo
$animalCOA;

$animalCOA->ksort();
echo
"After ksort():<br />";
echo
$animalCOA;

echo
"COA Count:<br />";
echo
$animalCOA->count();
echo
"<br />";
if (
$animalCOA->cleared()) {
    echo
"COA has an empty list";
} else {
    echo
"COA does not have an empty list";
}
echo
"<br />";
// Demonstrating different ways of adding a new value
// without providing a key.
$animalCOA->append('platypus');
$animalCOA->add('platypus');
$animalCOA->push('platypus');
$animalCOA->unshift('platypus');
$animalCOA[] = 'platypus';

// Demonstrating different ways of adding a new value
// when providing a key.
$animalCOA->offsetSet('key1', 'platypus');
$animalCOA->put('key2', 'platypus');
$animalCOA['key3'] = 'platypus';

// Demonstrate finding number of occurrences
// of a particular value in a list.
echo "Platypus occurs " . $animalCOA->occurrences('platypus') . " times in the list.";

// Retrieving values:
$animalCOA->offsetGet('key1');
$animalCOA->get('key1');
$animalCOA['key1'];

// Removing values:
$animalCOA->offsetUnset('key1');
$animalCOA->remove('key2');
$animalCOA->pop();
$animalCOA->shift();

// Resetting the list:
$animalCOA->clear();

?>