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 | |
| 8 | .. contents:: Page Contents |
| 9 | |
| 10 | Loading this Helper |
| 11 | =================== |
| 12 | |
| 13 | This helper is loaded using the following code |
| 14 | |
| 15 | :: |
| 16 | |
| 17 | $this->load->helper('array'); |
| 18 | |
| 19 | The following functions are available: |
| 20 | |
| 21 | element() |
| 22 | ========= |
| 23 | |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 24 | .. php:method:: element($item, $array, $default = NULL) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 25 | |
| 26 | :param string $item: Item to fetch from the array |
| 27 | :param array $array: Input array |
| 28 | :param boolean $default: What to return if the array isn't valid |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 29 | :returns: NULL on failure or the array item. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
| 31 | |
| 32 | Lets you fetch an item from an array. The function tests whether the |
| 33 | array index is set and whether it has a value. If a value exists it is |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 34 | returned. If a value does not exist it returns NULL, or whatever you've |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | specified as the default value via the third parameter. Example |
| 36 | |
| 37 | :: |
| 38 | |
| 39 | $array = array( |
| 40 | 'color' => 'red', |
| 41 | 'shape' => 'round', |
| 42 | 'size' => '' |
| 43 | ); |
| 44 | |
| 45 | echo element('color', $array); // returns "red" |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 46 | echo element('size', $array, 'foobar'); // returns "foobar" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | |
| 48 | elements() |
| 49 | ========== |
| 50 | |
| 51 | Lets you fetch a number of items from an array. The function tests |
| 52 | whether each of the array indices is set. If an index does not exist it |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 53 | is set to NULL, or whatever you've specified as the default value via |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | the third parameter. |
| 55 | |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 56 | .. php:method:: elements($items, $array, $default = NULL) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 57 | |
| 58 | :param string $item: Item to fetch from the array |
| 59 | :param array $array: Input array |
| 60 | :param boolean $default: What to return if the array isn't valid |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 61 | :returns: NULL on failure or the array item. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | |
| 63 | Example |
| 64 | |
| 65 | :: |
| 66 | |
| 67 | $array = array( |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 68 | 'color' => 'red', |
| 69 | 'shape' => 'round', |
| 70 | 'radius' => '10', |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 71 | 'diameter' => '20' |
| 72 | ); |
| 73 | |
| 74 | $my_shape = elements(array('color', 'shape', 'height'), $array); |
| 75 | |
| 76 | The above will return the following array |
| 77 | |
| 78 | :: |
| 79 | |
| 80 | array( |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 81 | 'color' => 'red', |
| 82 | 'shape' => 'round', |
| 83 | 'height' => NULL |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | ); |
| 85 | |
| 86 | You can set the third parameter to any default value you like |
| 87 | |
| 88 | :: |
| 89 | |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 90 | $my_shape = elements(array('color', 'shape', 'height'), $array, 'foobar'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 91 | |
| 92 | The above will return the following array |
| 93 | |
| 94 | :: |
| 95 | |
| 96 | array( |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 97 | 'color' => 'red', |
| 98 | 'shape' => 'round', |
| 99 | 'height' => 'foobar' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | ); |
| 101 | |
| 102 | This is useful when sending the $_POST array to one of your Models. |
| 103 | This prevents users from sending additional POST data to be entered into |
| 104 | your tables |
| 105 | |
| 106 | :: |
| 107 | |
| 108 | $this->load->model('post_model'); |
| 109 | $this->post_model->update( |
| 110 | elements(array('id', 'title', 'content'), $_POST) |
| 111 | ); |
| 112 | |
| 113 | This ensures that only the id, title and content fields are sent to be |
| 114 | updated. |
| 115 | |
| 116 | random_element() |
| 117 | ================ |
| 118 | |
| 119 | Takes an array as input and returns a random element from it. Usage |
| 120 | example |
| 121 | |
| 122 | .. php:method:: random_element($array) |
| 123 | |
| 124 | :param array $array: Input array |
| 125 | :returns: String - Random element from the array. |
| 126 | |
| 127 | :: |
| 128 | |
| 129 | $quotes = array( |
| 130 | "I find that the harder I work, the more luck I seem to have. - Thomas Jefferson", |
| 131 | "Don't stay in bed, unless you can make money in bed. - George Burns", |
| 132 | "We didn't lose the game; we just ran out of time. - Vince Lombardi", |
| 133 | "If everything seems under control, you're not going fast enough. - Mario Andretti", |
| 134 | "Reality is merely an illusion, albeit a very persistent one. - Albert Einstein", |
| 135 | "Chance favors the prepared mind - Louis Pasteur" |
| 136 | ); |
| 137 | |
| 138 | echo random_element($quotes); |
| 139 | |