PHP Classes

Aliased Array: Assign array entries that point to other entries

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 121 All time: 9,492 This week: 455Up
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: 79
All time rank: 3423460 in United States United States
Week rank: 200 Up22 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 (3)  
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 (3)  /  doc  
File Role Description
  Accessible without login Plain text file README Doc. Readme

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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:121
This week:0
All time:9,492
This week:455Up