PHP Classes

Aliased Array: Assign array entries that point to other entries

Recommend this page to a friend!
  Info   View files Documentation   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 121 This week: 1All time: 9,478 This week: 560Up
Version License PHP version Categories
aliased-array 15.6.8MIT/X Consortium ...5.0PHP 5, Data types
Description 

Author

This class can assign array entries that point to other entries.

It extends ArrayObject to add functions that allow telling that a value for a given entry is the same as another entry.

If the pointed entry does not exist, the class throws an exception.

Fetching the value of an alias entry returns the value of the entry that the alias points.

Innovation Award
PHP Programming Innovation award nominee
June 2015
Number 11


Prize: One subscription to the PDF edition of the PHP Architect magazine
Some applications need to build arrays that may contain many entries that have the same values.

This class that allows having an array with multiple entries pointing to the same value, rather than having different with entries with values that are the same.

Manuel Lemos
Picture of Richard Williams
  Performance   Level  
Name: Richard Williams <contact>
Classes: 2 packages by
Country: United States United States
Age: 78
All time rank: 3419460 in United States United States
Week rank: 416 Up46 in United States United States Up
Innovation award
Innovation award
Nominee: 1x

Documentation

* Extends ArrayObject to implement a aliased key array. Allows for any number of * unique keys (alias keys) pointing to an array element. * * CAUTION: This will not handle multi-dimension arrays like you can't * do this: * * $a = new AliasedArray(); * $a[1][2] = 25; * * This will not work and will not generate an error. * * To add an element you first have to add an indexed data element * * $a = new AliasedArray(); * $a->set(key, data); * $a[key] = data; * * You can also add an existing array: * * $a = new AliasedArray($existingArray); * * After an element has been added you can add an alias for the key. The * alias is not allowed to match a key. * * $a->alias(key, alias); * * The following would throw an exception: * * $a->set('a', 10); * $a->set('b', 20); * $a->alias('a', 'b'); * * because the alias 'a' is already used as a key. * If the key does not exist, an exception will be thrown. * So for example these will work: * * $a->set(1, 'thedata'); * $a->alias(1, 'theabc'); * or * $a['key1'] = 'newdata'; * $a->alias('key1', 'alias1'); * * To get the data * * $a->get(1); // returns 'thedata' * $a[1]; // ditto * $a['key1']; // returns 'newdata' * $a['alias1']; // ditto * * if the element for the key or the index does not exist then a * NULL will be returned. You can unset key and alias(es). If you unset * by key then the key/value will be unset and any aliases pointing to the * value will be unset. If you unset an alias only that alias will be unset. * * unset($a['key1']); * unset($a['alias1']); * * Iterators iterate across the key array and ignore the alias array.

  Files folder image Files  
File Role Description
Files folder imagedoc (1 file)
Files folder imagetests (1 file)
Plain text file AliasedArray.class.php Class Class source

  Files folder image Files  /  doc  
File Role Description
  Accessible without login Plain text file README Doc. Readme

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file AliasedArrayTest.php Test PHPUnit tests

 Version Control Unique User Downloads Download Rankings  
 0%
Total:121
This week:1
All time:9,478
This week:560Up