blob: 15b5e17c47dd717480b54208ca9797d7436ba6d9 [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
vlakoff0cd55352012-07-02 15:20:42 +020024.. php:method:: element($item, $array, $default = NULL)
Derek Jones8ede1a22011-10-05 13:34:52 -050025
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
vlakoff0cd55352012-07-02 15:20:42 +020029 :returns: NULL on failure or the array item.
Derek Jones8ede1a22011-10-05 13:34:52 -050030
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
vlakoff0cd55352012-07-02 15:20:42 +020034returned. If a value does not exist it returns NULL, or whatever you've
Derek Jones8ede1a22011-10-05 13:34:52 -050035specified 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"
vlakoff0cd55352012-07-02 15:20:42 +020046 echo element('size', $array, 'foobar'); // returns "foobar"
Derek Jones8ede1a22011-10-05 13:34:52 -050047
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
vlakoff0cd55352012-07-02 15:20:42 +020053is set to NULL, or whatever you've specified as the default value via
Derek Jones8ede1a22011-10-05 13:34:52 -050054the third parameter.
55
vlakoff0cd55352012-07-02 15:20:42 +020056.. php:method:: elements($items, $array, $default = NULL)
Derek Jones8ede1a22011-10-05 13:34:52 -050057
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
vlakoff0cd55352012-07-02 15:20:42 +020061 :returns: NULL on failure or the array item.
Derek Jones8ede1a22011-10-05 13:34:52 -050062
63Example
64
65::
66
67 $array = array(
vlakoff0cd55352012-07-02 15:20:42 +020068 'color' => 'red',
69 'shape' => 'round',
70 'radius' => '10',
Derek Jones8ede1a22011-10-05 13:34:52 -050071 '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(
vlakoff0cd55352012-07-02 15:20:42 +020081 'color' => 'red',
82 'shape' => 'round',
83 'height' => NULL
Derek Jones8ede1a22011-10-05 13:34:52 -050084 );
85
86You can set the third parameter to any default value you like
87
88::
89
vlakoff0cd55352012-07-02 15:20:42 +020090 $my_shape = elements(array('color', 'shape', 'height'), $array, 'foobar');
Derek Jones8ede1a22011-10-05 13:34:52 -050091
92The above will return the following array
93
94::
95
96 array(     
vlakoff0cd55352012-07-02 15:20:42 +020097 'color' => 'red',
98 'shape' => 'round',
99 'height' => 'foobar'
Derek Jones8ede1a22011-10-05 13:34:52 -0500100 );
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