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 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 29 | .. php: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. |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 35 | :rtype: mixed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 36 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 37 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 41 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 42 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 44 | $array = array( |
| 45 | 'color' => 'red', |
| 46 | 'shape' => 'round', |
| 47 | 'size' => '' |
| 48 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 49 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 50 | echo element('color', $array); // returns "red" |
| 51 | echo element('size', $array, 'foobar'); // returns "foobar" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 54 | .. php:function:: elements($items, $array[, $default = NULL]) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 55 | |
| 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 Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 60 | :rtype: mixed |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 61 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 62 | 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 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 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 68 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 69 | $array = array( |
| 70 | 'color' => 'red', |
| 71 | 'shape' => 'round', |
| 72 | 'radius' => '10', |
| 73 | 'diameter' => '20' |
| 74 | ); |
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 | $my_shape = elements(array('color', 'shape', 'height'), $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 | The above will return the following array:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 80 | array( |
| 81 | 'color' => 'red', |
| 82 | 'shape' => 'round', |
| 83 | 'height' => NULL |
| 84 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 85 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 86 | You can set the third parameter to any default value you like. |
| 87 | :: |
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 | $my_shape = elements(array('color', 'shape', 'height'), $array, 'foobar'); |
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 | The above will return the following array:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 93 | array( |
| 94 | 'color' => 'red', |
| 95 | 'shape' => 'round', |
| 96 | 'height' => 'foobar' |
| 97 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 99 | 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 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 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 104 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 105 | $this->load->model('post_model'); |
| 106 | $this->post_model->update( |
| 107 | elements(array('id', 'title', 'content'), $_POST) |
| 108 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 109 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 110 | This ensures that only the id, title and content fields are sent to be |
| 111 | updated. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 113 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 114 | .. php:function:: random_element($array) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 115 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 116 | :param array $array: Input array |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 117 | :returns: A random element from the array |
| 118 | :rtype: mixed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 120 | Takes an array as input and returns a random element from it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 122 | Usage example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 123 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 124 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | |
Derek Jones | e07b4e7 | 2013-07-19 16:14:38 -0700 | [diff] [blame] | 133 | echo random_element($quotes); |