summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/helpers/array_helper.rst
blob: 9435b3ac77d4c6686b0ca30158c3b97357eeb775 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
############
Array Helper
############

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

.. contents:: Page Contents

Loading this Helper
===================

This helper is loaded using the following code::

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

The following functions are available:

element()
=========

.. php:function:: element($item, $array, $default = NULL)

	:param	string	$item: Item to fetch from the array
	:param	array	$array: Input array
	:param	bool	$default: What to return if the array isn't valid
	:returns:	NULL on failure or the array item.

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::

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

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

elements()
==========

.. php:function:: elements($items, $array, $default = NULL)

	:param	string	$item: Item to fetch from the array
	:param	array	$array: Input array
	:param	bool	$default: What to return if the array isn't valid
	:returns:	NULL on failure or the array item.

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::

	$array = array(
		'color' => 'red',
		'shape' => 'round',
		'radius' => '10',
		'diameter' => '20'
	);

	$my_shape = elements(array('color', 'shape', 'height'), $array);

The above will return the following array::

	array(
		'color' => 'red',
		'shape' => 'round',
		'height' => NULL
	);

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

	 $my_shape = elements(array('color', 'shape', 'height'), $array, 'foobar');

The above will return the following array::

	array(     
		'color' 	=> 'red',
		'shape' 	=> 'round',
		'height'	=> 'foobar'
	);

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->load->model('post_model');
	$this->post_model->update(
		elements(array('id', 'title', 'content'), $_POST)
	);

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

random_element()
================

.. php:function:: random_element($array)

	:param	array	$array: Input array
	:returns:	string (a random element from the array)

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);