blob: 20f5864a6e314b68d7c708d9b96778c7ade3d819 [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
Derek Jonese07b4e72013-07-19 16:14:38 -070029.. 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.
Derek Jones8ede1a22011-10-05 13:34:52 -050035
Derek Jonese07b4e72013-07-19 16:14:38 -070036 Lets you fetch an item from an array. The function tests whether the
37 array index is set and whether it has a value. If a value exists it is
38 returned. If a value does not exist it returns NULL, or whatever you've
39 specified as the default value via the third parameter.
Derek Jones8ede1a22011-10-05 13:34:52 -050040
Derek Jonese07b4e72013-07-19 16:14:38 -070041 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050042
Derek Jonese07b4e72013-07-19 16:14:38 -070043 $array = array(
44 'color' => 'red',
45 'shape' => 'round',
46 'size' => ''
47 );
Derek Jones8ede1a22011-10-05 13:34:52 -050048
Derek Jonese07b4e72013-07-19 16:14:38 -070049 echo element('color', $array); // returns "red"
50 echo element('size', $array, 'foobar'); // returns "foobar"
Derek Jones8ede1a22011-10-05 13:34:52 -050051
Derek Jones8ede1a22011-10-05 13:34:52 -050052
Derek Jonese07b4e72013-07-19 16:14:38 -070053.. function:: elements($items, $array[, $default = NULL])
Andrey Andreev48a86752012-11-08 15:16:34 +020054
55 :param string $item: Item to fetch from the array
56 :param array $array: Input array
57 :param bool $default: What to return if the array isn't valid
58 :returns: NULL on failure or the array item.
59
Derek Jonese07b4e72013-07-19 16:14:38 -070060 Lets you fetch a number of items from an array. The function tests
61 whether each of the array indices is set. If an index does not exist it
62 is set to NULL, or whatever you've specified as the default value via
63 the third parameter.
Derek Jones8ede1a22011-10-05 13:34:52 -050064
Derek Jonese07b4e72013-07-19 16:14:38 -070065 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050066
Derek Jonese07b4e72013-07-19 16:14:38 -070067 $array = array(
68 'color' => 'red',
69 'shape' => 'round',
70 'radius' => '10',
71 'diameter' => '20'
72 );
Derek Jones8ede1a22011-10-05 13:34:52 -050073
Derek Jonese07b4e72013-07-19 16:14:38 -070074 $my_shape = elements(array('color', 'shape', 'height'), $array);
Derek Jones8ede1a22011-10-05 13:34:52 -050075
Derek Jonese07b4e72013-07-19 16:14:38 -070076 The above will return the following array::
Derek Jones8ede1a22011-10-05 13:34:52 -050077
Derek Jonese07b4e72013-07-19 16:14:38 -070078 array(
79 'color' => 'red',
80 'shape' => 'round',
81 'height' => NULL
82 );
Derek Jones8ede1a22011-10-05 13:34:52 -050083
Derek Jonese07b4e72013-07-19 16:14:38 -070084 You can set the third parameter to any default value you like.
85 ::
Derek Jones8ede1a22011-10-05 13:34:52 -050086
Derek Jonese07b4e72013-07-19 16:14:38 -070087 $my_shape = elements(array('color', 'shape', 'height'), $array, 'foobar');
Derek Jones8ede1a22011-10-05 13:34:52 -050088
Derek Jonese07b4e72013-07-19 16:14:38 -070089 The above will return the following array::
Derek Jones8ede1a22011-10-05 13:34:52 -050090
Derek Jonese07b4e72013-07-19 16:14:38 -070091 array(     
92 'color' => 'red',
93 'shape' => 'round',
94 'height' => 'foobar'
95 );
Derek Jones8ede1a22011-10-05 13:34:52 -050096
Derek Jonese07b4e72013-07-19 16:14:38 -070097 This is useful when sending the ``$_POST`` array to one of your Models.
98 This prevents users from sending additional POST data to be entered into
99 your tables.
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
Derek Jonese07b4e72013-07-19 16:14:38 -0700101 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500102
Derek Jonese07b4e72013-07-19 16:14:38 -0700103 $this->load->model('post_model');
104 $this->post_model->update(
105 elements(array('id', 'title', 'content'), $_POST)
106 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Derek Jonese07b4e72013-07-19 16:14:38 -0700108 This ensures that only the id, title and content fields are sent to be
109 updated.
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
Derek Jonesb8c283a2013-07-19 16:02:53 -0700112.. function:: random_element($array)
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Andrey Andreev48a86752012-11-08 15:16:34 +0200114 :param array $array: Input array
115 :returns: string (a random element from the array)
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Derek Jonese07b4e72013-07-19 16:14:38 -0700117 Takes an array as input and returns a random element from it.
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
Derek Jonese07b4e72013-07-19 16:14:38 -0700119 Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Derek Jonese07b4e72013-07-19 16:14:38 -0700121 $quotes = array(
122 "I find that the harder I work, the more luck I seem to have. - Thomas Jefferson",
123 "Don't stay in bed, unless you can make money in bed. - George Burns",
124 "We didn't lose the game; we just ran out of time. - Vince Lombardi",
125 "If everything seems under control, you're not going fast enough. - Mario Andretti",
126 "Reality is merely an illusion, albeit a very persistent one. - Albert Einstein",
127 "Chance favors the prepared mind - Louis Pasteur"
128 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Derek Jonese07b4e72013-07-19 16:14:38 -0700130 echo random_element($quotes);