blob: d6b48773f45d540b03b1520b1b090723626b0419 [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
Derek Jonese07b4e72013-07-19 16:14:38 -07008.. contents::
9 :local:
10
11.. raw:: html
12
13 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15Loading this Helper
16===================
17
Andrey Andreev48a86752012-11-08 15:16:34 +020018This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050019
20 $this->load->helper('array');
21
Derek Jonese07b4e72013-07-19 16:14:38 -070022
23Available Functions
24===================
25
Derek Jones8ede1a22011-10-05 13:34:52 -050026The following functions are available:
27
Derek Jones8ede1a22011-10-05 13:34:52 -050028
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020029.. php:function:: element($item, $array[, $default = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050030
Andrey Andreev48a86752012-11-08 15:16:34 +020031 :param string $item: Item to fetch from the array
32 :param array $array: Input array
33 :param bool $default: What to return if the array isn't valid
34 :returns: NULL on failure or the array item.
Andrey Andreev3de130c2014-02-07 23:31:49 +020035 :rtype: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -050036
Derek Jonese07b4e72013-07-19 16:14:38 -070037 Lets you fetch an item from an array. The function tests whether the
38 array index is set and whether it has a value. If a value exists it is
39 returned. If a value does not exist it returns NULL, or whatever you've
40 specified as the default value via the third parameter.
Derek Jones8ede1a22011-10-05 13:34:52 -050041
Derek Jonese07b4e72013-07-19 16:14:38 -070042 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Derek Jonese07b4e72013-07-19 16:14:38 -070044 $array = array(
45 'color' => 'red',
46 'shape' => 'round',
47 'size' => ''
48 );
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Derek Jonese07b4e72013-07-19 16:14:38 -070050 echo element('color', $array); // returns "red"
51 echo element('size', $array, 'foobar'); // returns "foobar"
Derek Jones8ede1a22011-10-05 13:34:52 -050052
Derek Jones8ede1a22011-10-05 13:34:52 -050053
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020054.. php:function:: elements($items, $array[, $default = NULL])
Andrey Andreev48a86752012-11-08 15:16:34 +020055
56 :param string $item: Item to fetch from the array
57 :param array $array: Input array
58 :param bool $default: What to return if the array isn't valid
59 :returns: NULL on failure or the array item.
Andrey Andreev3de130c2014-02-07 23:31:49 +020060 :rtype: mixed
Andrey Andreev48a86752012-11-08 15:16:34 +020061
Derek Jonese07b4e72013-07-19 16:14:38 -070062 Lets you fetch a number of items from an array. The function tests
63 whether each of the array indices is set. If an index does not exist it
64 is set to NULL, or whatever you've specified as the default value via
65 the third parameter.
Derek Jones8ede1a22011-10-05 13:34:52 -050066
Derek Jonese07b4e72013-07-19 16:14:38 -070067 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050068
Derek Jonese07b4e72013-07-19 16:14:38 -070069 $array = array(
70 'color' => 'red',
71 'shape' => 'round',
72 'radius' => '10',
73 'diameter' => '20'
74 );
Derek Jones8ede1a22011-10-05 13:34:52 -050075
Derek Jonese07b4e72013-07-19 16:14:38 -070076 $my_shape = elements(array('color', 'shape', 'height'), $array);
Derek Jones8ede1a22011-10-05 13:34:52 -050077
Derek Jonese07b4e72013-07-19 16:14:38 -070078 The above will return the following array::
Derek Jones8ede1a22011-10-05 13:34:52 -050079
Derek Jonese07b4e72013-07-19 16:14:38 -070080 array(
81 'color' => 'red',
82 'shape' => 'round',
83 'height' => NULL
84 );
Derek Jones8ede1a22011-10-05 13:34:52 -050085
Derek Jonese07b4e72013-07-19 16:14:38 -070086 You can set the third parameter to any default value you like.
87 ::
Derek Jones8ede1a22011-10-05 13:34:52 -050088
Derek Jonese07b4e72013-07-19 16:14:38 -070089 $my_shape = elements(array('color', 'shape', 'height'), $array, 'foobar');
Derek Jones8ede1a22011-10-05 13:34:52 -050090
Derek Jonese07b4e72013-07-19 16:14:38 -070091 The above will return the following array::
Derek Jones8ede1a22011-10-05 13:34:52 -050092
Derek Jonese07b4e72013-07-19 16:14:38 -070093 array(     
94 'color' => 'red',
95 'shape' => 'round',
96 'height' => 'foobar'
97 );
Derek Jones8ede1a22011-10-05 13:34:52 -050098
Derek Jonese07b4e72013-07-19 16:14:38 -070099 This is useful when sending the ``$_POST`` array to one of your Models.
100 This prevents users from sending additional POST data to be entered into
101 your tables.
Derek Jones8ede1a22011-10-05 13:34:52 -0500102
Derek Jonese07b4e72013-07-19 16:14:38 -0700103 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
Derek Jonese07b4e72013-07-19 16:14:38 -0700105 $this->load->model('post_model');
106 $this->post_model->update(
107 elements(array('id', 'title', 'content'), $_POST)
108 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
Derek Jonese07b4e72013-07-19 16:14:38 -0700110 This ensures that only the id, title and content fields are sent to be
111 updated.
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200114.. php:function:: random_element($array)
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Andrey Andreev48a86752012-11-08 15:16:34 +0200116 :param array $array: Input array
Andrey Andreev3de130c2014-02-07 23:31:49 +0200117 :returns: A random element from the array
118 :rtype: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Derek Jonese07b4e72013-07-19 16:14:38 -0700120 Takes an array as input and returns a random element from it.
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Derek Jonese07b4e72013-07-19 16:14:38 -0700122 Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500123
Derek Jonese07b4e72013-07-19 16:14:38 -0700124 $quotes = array(
125 "I find that the harder I work, the more luck I seem to have. - Thomas Jefferson",
126 "Don't stay in bed, unless you can make money in bed. - George Burns",
127 "We didn't lose the game; we just ran out of time. - Vince Lombardi",
128 "If everything seems under control, you're not going fast enough. - Mario Andretti",
129 "Reality is merely an illusion, albeit a very persistent one. - Albert Einstein",
130 "Chance favors the prepared mind - Louis Pasteur"
131 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
Derek Jonese07b4e72013-07-19 16:14:38 -0700133 echo random_element($quotes);