PHP Classes

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

Recommend this page to a friend!
     
  Info   Example   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: 14 All time: 11,373 This week: 560Up
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: 16206 in Serbia Serbia
Week rank: 215 Up1 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 (6)  
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 (6)  /  ArrayList  
File Role Description
  Plain text file ArrayList.php Class Class source
  Plain text file IArrayList.php Class Class source

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

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

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  
 100%
Total:14
This week:0
All time:11,373
This week:560Up