CodeIgniter User Guide Version 1.6.0.1


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:

$this->load->helper('array');

The following functions are available:

element()

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 FALSE, or whatever you've specified as the default value via the third parameter. Example:

$array = array('color' => 'red', 'shape' => 'round', 'size' => '');

// returns "red"
echo element('color', $array);

// returns NULL
echo element('size', $array, NULL);

random_element()

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

$quotes = array(
            "I find that the harder I work, the more luck I seem to have. - Thomas Jefferson",
            "Don't stay in bed, unless you can make money in bed. - George Burns",
            "We didn't lose the game; we just ran out of time. - Vince Lombardi",
            "If everything seems under control, you're not going fast enough. - Mario Andretti",
            "Reality is merely an illusion, albeit a very persistent one. - Albert Einstein",
            "Chance favors the prepared mind - Louis Pasteur"
            );

echo random_element($quotes);