PHP Classes

PHP Hash Array Collection: Create arrays that may have keys of any type

Recommend this page to a friend!
  Info   View files Example   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 13 All time: 11,232 This week: 340Up
Version License PHP version Categories
hash-collection 1.0BSD License7.3PHP 5, Data types
Description 

Author

This package allows to create arrays that may have keys of any type.

It defines a new type of array that can store values to be associated with keys that can be of any type of data values

The class allows for instance objects to be used as array keys besides integers or strings as with regular arrays.

Picture of Aleksandar Zivanovic
  Performance   Level  
Name: Aleksandar Zivanovic <contact>
Classes: 16 packages by
Country: Serbia Serbia
Age: 30
All time rank: 16237 in Serbia Serbia
Week rank: 312 Up2 in Serbia Serbia Up
Innovation award
Innovation award
Nominee: 4x

Example

<?php

require_once __DIR__ . '/Exceptions/HashException.php';

require_once
__DIR__ . '/Hash/IHash.php';
require_once
__DIR__ . '/ArrayList/IArrayList.php';

require_once
__DIR__ . '/Hash/Hash.php';
require_once
__DIR__ . '/ArrayList/ArrayList.php';

$hashCapacity = 10;
$hash = new Hash(stdClass::class, int::class, $hashCapacity);

$users = [];

for (
$userId = 0; $userId < $hashCapacity - 1; $userId++) {
   
$user = new stdClass();
   
$users[] = $user;
   
$hash[$user] = $userId + 1;
}

echo
"{$hash[$users[0]]}\n"; // This will print number for first "user" key

try {
   
$hash[new stdClass()] = 'invalid value exception will be thrown';
} catch (
HashException $exception) {
    echo
"{$exception->getMessage()}\n";
}

try {
   
$hash['invalid key exception will be thrown'] = 1;
} catch (
HashException $exception) {
    echo
"{$exception->getMessage()}\n";
}

try {
   
$hash[new stdClass()] = 0;
   
$hash[new stdClass()] = 1;
} catch (
HashException $exception) {
    echo
"{$exception->getMessage()}\n";
}


  Files folder image Files  
File Role Description
Files folder imageArrayList (2 files)
Files folder imageExceptions (1 file)
Files folder imageHash (2 files)
Accessible without login Plain text file example.php Example Example script

  Files folder image Files  /  ArrayList  
File Role Description
  Plain text file ArrayList.php Class Class source
  Plain text file IArrayList.php Class Class source

  Files folder image Files  /  Exceptions  
File Role Description
  Plain text file HashException.php Class Class source

  Files folder image Files  /  Hash  
File Role Description
  Plain text file Hash.php Class Class source
  Plain text file IHash.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:13
This week:0
All time:11,232
This week:340Up