blob: 4308753bb083751b4821cbf7b24bfea032e23398 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001############
2Array Helper
3############
4
5The Array Helper file contains functions that assist in working with
6arrays.
7
8.. contents:: Page Contents
9
10Loading this Helper
11===================
12
13This helper is loaded using the following code
14
15::
16
17 $this->load->helper('array');
18
19The following functions are available:
20
21element()
22=========
23
24.. php:method:: element($item, $array, $default = FALSE)
25
26 :param string $item: Item to fetch from the array
27 :param array $array: Input array
28 :param boolean $default: What to return if the array isn't valid
29 :returns: FALSE on failure or the array item.
30
31
32Lets you fetch an item from an array. The function tests whether the
33array index is set and whether it has a value. If a value exists it is
34returned. If a value does not exist it returns FALSE, or whatever you've
35specified as the default value via the third parameter. Example
36
37::
38
39 $array = array(
40 'color' => 'red',
41 'shape' => 'round',
42 'size' => ''
43 );
44
45 echo element('color', $array); // returns "red"
46 echo element('size', $array, NULL); // returns NULL
47
48elements()
49==========
50
51Lets you fetch a number of items from an array. The function tests
52whether each of the array indices is set. If an index does not exist it
53is set to FALSE, or whatever you've specified as the default value via
54the third parameter.
55
56.. php:method:: elements($items, $array, $default = FALSE)
57
58 :param string $item: Item to fetch from the array
59 :param array $array: Input array
60 :param boolean $default: What to return if the array isn't valid
61 :returns: FALSE on failure or the array item.
62
63Example
64
65::
66
67 $array = array(
68 'color' => 'red',  
69 'shape' => 'round',     
70 'radius' => '10',     
71 'diameter' => '20'
72 );
73
74 $my_shape = elements(array('color', 'shape', 'height'), $array);
75
76The above will return the following array
77
78::
79
80 array(
81 'color' => 'red',     
82 'shape' => 'round',     
83 'height' => FALSE
84 );
85
86You can set the third parameter to any default value you like
87
88::
89
90 $my_shape = elements(array('color', 'shape', 'height'), $array, NULL);
91
92The above will return the following array
93
94::
95
96 array(     
97 'color' => 'red',     
98 'shape' => 'round',     
99 'height' => NULL
100 );
101
102This is useful when sending the $_POST array to one of your Models.
103This prevents users from sending additional POST data to be entered into
104your tables
105
106::
107
108 $this->load->model('post_model');
109 $this->post_model->update(
110 elements(array('id', 'title', 'content'), $_POST)
111 );
112
113This ensures that only the id, title and content fields are sent to be
114updated.
115
116random_element()
117================
118
119Takes an array as input and returns a random element from it. Usage
120example
121
122.. php:method:: random_element($array)
123
124 :param array $array: Input array
125 :returns: String - Random element from the array.
126
127::
128
129 $quotes = array(
130 "I find that the harder I work, the more luck I seem to have. - Thomas Jefferson",
131 "Don't stay in bed, unless you can make money in bed. - George Burns",
132 "We didn't lose the game; we just ran out of time. - Vince Lombardi",
133 "If everything seems under control, you're not going fast enough. - Mario Andretti",
134 "Reality is merely an illusion, albeit a very persistent one. - Albert Einstein",
135 "Chance favors the prepared mind - Louis Pasteur"
136 );
137
138 echo random_element($quotes);
139