blob: 9435b3ac77d4c6686b0ca30158c3b97357eeb775 [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
Andrey Andreev48a86752012-11-08 15:16:34 +020013This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15 $this->load->helper('array');
16
17The following functions are available:
18
19element()
20=========
21
Andrey Andreev48a86752012-11-08 15:16:34 +020022.. php:function:: element($item, $array, $default = NULL)
Derek Jones8ede1a22011-10-05 13:34:52 -050023
Andrey Andreev48a86752012-11-08 15:16:34 +020024 :param string $item: Item to fetch from the array
25 :param array $array: Input array
26 :param bool $default: What to return if the array isn't valid
27 :returns: NULL on failure or the array item.
Derek Jones8ede1a22011-10-05 13:34:52 -050028
29Lets you fetch an item from an array. The function tests whether the
30array index is set and whether it has a value. If a value exists it is
vlakoff0cd55352012-07-02 15:20:42 +020031returned. If a value does not exist it returns NULL, or whatever you've
Andrey Andreev48a86752012-11-08 15:16:34 +020032specified as the default value via the third parameter.
Derek Jones8ede1a22011-10-05 13:34:52 -050033
Andrey Andreev48a86752012-11-08 15:16:34 +020034Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050035
36 $array = array(
37 'color' => 'red',
38 'shape' => 'round',
39 'size' => ''
40 );
41
42 echo element('color', $array); // returns "red"
vlakoff0cd55352012-07-02 15:20:42 +020043 echo element('size', $array, 'foobar'); // returns "foobar"
Derek Jones8ede1a22011-10-05 13:34:52 -050044
45elements()
46==========
47
Andrey Andreev48a86752012-11-08 15:16:34 +020048.. php:function:: elements($items, $array, $default = NULL)
49
50 :param string $item: Item to fetch from the array
51 :param array $array: Input array
52 :param bool $default: What to return if the array isn't valid
53 :returns: NULL on failure or the array item.
54
Derek Jones8ede1a22011-10-05 13:34:52 -050055Lets you fetch a number of items from an array. The function tests
56whether each of the array indices is set. If an index does not exist it
vlakoff0cd55352012-07-02 15:20:42 +020057is set to NULL, or whatever you've specified as the default value via
Derek Jones8ede1a22011-10-05 13:34:52 -050058the third parameter.
59
Andrey Andreev48a86752012-11-08 15:16:34 +020060Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050061
62 $array = array(
vlakoff0cd55352012-07-02 15:20:42 +020063 'color' => 'red',
64 'shape' => 'round',
65 'radius' => '10',
Derek Jones8ede1a22011-10-05 13:34:52 -050066 'diameter' => '20'
67 );
68
69 $my_shape = elements(array('color', 'shape', 'height'), $array);
70
Andrey Andreev48a86752012-11-08 15:16:34 +020071The above will return the following array::
Derek Jones8ede1a22011-10-05 13:34:52 -050072
73 array(
vlakoff0cd55352012-07-02 15:20:42 +020074 'color' => 'red',
75 'shape' => 'round',
76 'height' => NULL
Derek Jones8ede1a22011-10-05 13:34:52 -050077 );
78
Andrey Andreev48a86752012-11-08 15:16:34 +020079You can set the third parameter to any default value you like.
Derek Jones8ede1a22011-10-05 13:34:52 -050080::
81
vlakoff0cd55352012-07-02 15:20:42 +020082 $my_shape = elements(array('color', 'shape', 'height'), $array, 'foobar');
Derek Jones8ede1a22011-10-05 13:34:52 -050083
Andrey Andreev48a86752012-11-08 15:16:34 +020084The above will return the following array::
Derek Jones8ede1a22011-10-05 13:34:52 -050085
86 array(     
vlakoff0cd55352012-07-02 15:20:42 +020087 'color' => 'red',
88 'shape' => 'round',
89 'height' => 'foobar'
Derek Jones8ede1a22011-10-05 13:34:52 -050090 );
91
Andrey Andreev48a86752012-11-08 15:16:34 +020092This is useful when sending the ``$_POST`` array to one of your Models.
Derek Jones8ede1a22011-10-05 13:34:52 -050093This prevents users from sending additional POST data to be entered into
Andrey Andreev48a86752012-11-08 15:16:34 +020094your tables.
Derek Jones8ede1a22011-10-05 13:34:52 -050095
96::
97
98 $this->load->model('post_model');
99 $this->post_model->update(
100 elements(array('id', 'title', 'content'), $_POST)
101 );
102
103This ensures that only the id, title and content fields are sent to be
104updated.
105
106random_element()
107================
108
Andrey Andreev48a86752012-11-08 15:16:34 +0200109.. php:function:: random_element($array)
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Andrey Andreev48a86752012-11-08 15:16:34 +0200111 :param array $array: Input array
112 :returns: string (a random element from the array)
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Andrey Andreev48a86752012-11-08 15:16:34 +0200114Takes an array as input and returns a random element from it.
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Andrey Andreev48a86752012-11-08 15:16:34 +0200116Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
118 $quotes = array(
119 "I find that the harder I work, the more luck I seem to have. - Thomas Jefferson",
120 "Don't stay in bed, unless you can make money in bed. - George Burns",
121 "We didn't lose the game; we just ran out of time. - Vince Lombardi",
122 "If everything seems under control, you're not going fast enough. - Mario Andretti",
123 "Reality is merely an illusion, albeit a very persistent one. - Albert Einstein",
124 "Chance favors the prepared mind - Louis Pasteur"
125 );
126
Andrey Andreev48a86752012-11-08 15:16:34 +0200127 echo random_element($quotes);