Hash Class: Makes CakePHP Coding easier!

Hash Class: Makes CakePHP Coding easier!
Social sharing

Hash is a predefined class provided by CakePHP. It is used for array manipulation such as inserting an element to an array, remove an element from an array, sort an array, extract part of a large array, filter the non empty elements, rearrange the whole array, which makes the code more optimized and understandable. So it makes CakePHP easier and flexible to use. Because most of the operations like find, insert, update in CakePHP returns/takes array as output/input.

Hash provides an improved interface, more consistent and predictable set of features over Set. While it lacks the spotty support for pseudo XPath, its more fully featured dot notation provides similar features in a more consistent implementation.

Operations performed by Hash class:

  • extract()
  • combine()
  • filter()
  • check()
  • insert()
  • remove()
  • sort()
  • and many more…

Some Important Tips:

{n} – Match any numeric key.
{s} – Match any string value and numeric string values.
[id] – Matches elements with a given array key.
[id=2] – Matches elements with id equal to 2.
[id!=2] – Matches elements with id not equal to 2.
[id<=2] – Matches elements with id less than or equal to 2.

– Matches elements that have values matching the regular expression inside.

  • Hash::extract(array $data, $path):

Retrieve required data from array. You do not have to loop through the array.

Ex: // Common Usage:

$users = $this->User->find("all");
 $results = Hash::extract($user, '{n}.User.id');
 // $results equals:
 // array(1,2,3,4,5,...);
  • Hash::insert(array $data, $path, $values = null):

Insert sub-array’s into the original array.

Ex:

$temp = array(
 'page' => array('name' => 'page')
);
$result = Hash::insert($temp, 'file', array('name' => 'files'));
// $result now looks like:
Array
(
 [page] => Array
 (
 [name] => page
 )
 [file] => Array
 (
 [name] => files
 )
)
  • Hash::remove(array $data,  $path = null):
Remove data from any path specified.

EX:

$arr_test = array(
 'page' => array('name' => 'page'),
 'file' => array('name' => 'files')
);
$result = Hash::remove($arr_test, 'file');
/* $result now looks like:
 Array
 (
 [page] => Array
 (
 [name] => page
 )
 
)
*/
  • Hash::combine(array $data$keyPath = null$valuePath = null$groupPath = null)

Combine the results to form a new array of expected result. Helpful in case, where we are displaying data in the form of select box. Like categories, states, city etc. We don’t have to retrieve the data separately. We can find the required data from the original result set retrieved, containing the information..

Ex:

$arr_test = array(
 array(
 'User' => array(
 'id' => 2,
 'group_id' => 1,
 'Data' => array(
 'user' => 'John.doe',
 'name' => 'Matt Lee'
 )
 )
 ),
 array(
 'User' => array(
 'id' => 14,
 'group_id' => 2,
 'Data' => array(
 'user' => 'phpunt',
 'name' => 'Jack'
 )
 )
 ),
);
 
$result = Hash::combine($arr_test, '{n}.User.id', '{n}.User.Data');
/* $result now looks like below:
 Array
 (
 [2] => Array
 (
 [user] => John.doe
 [name] => Matt Lee
 )
 [14] => Array
 (
 [user] => phpunt
 [name] => Jack
 )
 )
*/
  • Hash::check(array $datastring $path = null)
    Check whether an element exists in the array or not.

Ex:

$set = array(
 'My Index' => array('First' => 'The first item1')
);
$result = Hash::check($set, 'My Index.First');
// $result == True
  • Hash::filter(array $data$callback = array(‘Hash ‘‘Filter’)):
Keep only non-empty elements and filter the empty elements.
Ex:
$data_arr = array(
 '0',
 false,
 true,
 0,
 array('test', 'is you own', false)
);
$result = Hash::filter($data_arr);
 
/* Out put:
 Array (
 [0] => 0
 [2] => true
 [3] => 0
 [4] => Array
 (
 [0] => one thing
 [2] => is you own
 )
 )
*/
  • Hash::sort(array $data$path$dir$type = ‘regular’)

Sort an array according to the path, direction and type provided.

Ex:

$arr_test = array(
 0 => array('Person' => array('name' => 'Jeff1')),
 1 => array('Shirt' => array('color' => 'black1'))
);
$result = Hash::sort($arr_test, '{n}.Person.name', 'asc');
/* $result now looks like:
 Array
 (
 [0] => Array
 (
 [Shirt] => Array
 (
 [color] => black1
 )
 )
 [1] => Array
 (
 [Person] => Array
 (
 [name] => Jeff1
 )
 )
 )
*/

$type can be of the following type:

regular
numeric
string
natural(ex. Will sort fooo10 below fooo2 as an example)

$dirc can be of two type asc & desc

For more information refer: http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html

However if you want you can hire or get free consultation from our experienced CakePHP developers.

Read More: Password Hashing API in PHP

Have I missed out anything? Comment at end of this topic.

Your recently viewed posts:

    Contact Us

    We’d love to help & work with you




    When do you want to start ?


    Enter your email address to stay up to date with the latest news.
    Holler Box

    Orange Exit pop up

    Subscribe for the latest
    trends in web and
    mobile app development
    Holler Box

    Exit pop up

    Sad to see you leaving early...

    From "Aha" to "Oh shit" we are sharing everything on our journey.
    Enter your email address to stay up to date with the latest news.
    Holler Box