Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############ |
| 2 | Array Helper |
| 3 | ############ |
| 4 | |
| 5 | The Array Helper file contains functions that assist in working with |
| 6 | arrays. |
| 7 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 8 | .. contents:: |
| 9 | :local: |
| 10 | |
| 11 | .. raw:: html |
| 12 | |
| 13 | <div class="custom-index container"></div> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 14 | |
| 15 | Loading this Helper |
| 16 | =================== |
| 17 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 18 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 19 | |
| 20 | $this->load->helper('array'); |
| 21 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 22 | |
| 23 | Available Functions |
| 24 | =================== |
| 25 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | The following functions are available: |
| 27 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 28 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 29 | .. function:: element($item, $array[, $default = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 31 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 36 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 41 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 42 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 43 | $array = array( |
| 44 | 'color' => 'red', |
| 45 | 'shape' => 'round', |
| 46 | 'size' => '' |
| 47 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 49 | echo element('color', $array); // returns "red" |
| 50 | echo element('size', $array, 'foobar'); // returns "foobar" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 53 | .. function:: elements($items, $array[, $default = NULL]) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 54 | |
| 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 Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 60 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 65 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 67 | $array = array( |
| 68 | 'color' => 'red', |
| 69 | 'shape' => 'round', |
| 70 | 'radius' => '10', |
| 71 | 'diameter' => '20' |
| 72 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 73 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 74 | $my_shape = elements(array('color', 'shape', 'height'), $array); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 76 | The above will return the following array:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 78 | array( |
| 79 | 'color' => 'red', |
| 80 | 'shape' => 'round', |
| 81 | 'height' => NULL |
| 82 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 84 | You can set the third parameter to any default value you like. |
| 85 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 87 | $my_shape = elements(array('color', 'shape', 'height'), $array, 'foobar'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 89 | The above will return the following array:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 90 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 91 | array( |
| 92 | 'color' => 'red', |
| 93 | 'shape' => 'round', |
| 94 | 'height' => 'foobar' |
| 95 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 96 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 97 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 101 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 102 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 103 | $this->load->model('post_model'); |
| 104 | $this->post_model->update( |
| 105 | elements(array('id', 'title', 'content'), $_POST) |
| 106 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 108 | This ensures that only the id, title and content fields are sent to be |
| 109 | updated. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 111 | |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 112 | .. function:: random_element($array) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 113 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 114 | :param array $array: Input array |
| 115 | :returns: string (a random element from the array) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 116 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 117 | Takes an array as input and returns a random element from it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 119 | Usage example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 121 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 129 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 130 | echo random_element($quotes); |