Array Helper

Bởi Nhan Duyen vào January 2016

Array Helper

The Array Helper file contains functions that assist in working with arrays.

Loading this Helper

This helper is loaded using the following code:

Available Functions

The following functions are available:

element($item, $array[, $default = NULL])
Parameters:
  • $item (string) – Item to fetch from the array
  • $array (array) – Input array
  • $default (bool) – What to return if the array isn’t valid
Returns:

NULL on failure or the array item.

Return type:

mixed

Lets you fetch an item from an array. The function tests whether the array index is set and whether it has a value. If a value exists it is returned. If a value does not exist it returns NULL, or whatever you’ve specified as the default value via the third parameter.

Example:

elements($items, $array[, $default = NULL])
Parameters:
  • $item (string) – Item to fetch from the array
  • $array (array) – Input array
  • $default (bool) – What to return if the array isn’t valid
Returns:

NULL on failure or the array item.

Return type:

mixed

Lets you fetch a number of items from an array. The function tests whether each of the array indices is set. If an index does not exist it is set to NULL, or whatever you’ve specified as the default value via the third parameter.

Example:

The above will return the following array:

You can set the third parameter to any default value you like.

The above will return the following array:

This is useful when sending the $_POST array to one of your Models. This prevents users from sending additional POST data to be entered into your tables.

This ensures that only the id, title and content fields are sent to be updated.

random_element($array)
Parameters:
  • $array (array) – Input array
Returns:

A random element from the array

Return type:

mixed

Takes an array as input and returns a random element from it.

Usage example: