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 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 13 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 14 | |
| 15 | $this->load->helper('array'); |
| 16 | |
| 17 | The following functions are available: |
| 18 | |
| 19 | element() |
| 20 | ========= |
| 21 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 22 | .. php:function:: element($item, $array, $default = NULL) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 23 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 24 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 28 | |
| 29 | Lets you fetch an item from an array. The function tests whether the |
| 30 | 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] | 31 | returned. If a value does not exist it returns NULL, or whatever you've |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 32 | specified as the default value via the third parameter. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 33 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 34 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | |
| 36 | $array = array( |
| 37 | 'color' => 'red', |
| 38 | 'shape' => 'round', |
| 39 | 'size' => '' |
| 40 | ); |
| 41 | |
| 42 | echo element('color', $array); // returns "red" |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 43 | echo element('size', $array, 'foobar'); // returns "foobar" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | |
| 45 | elements() |
| 46 | ========== |
| 47 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 48 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 55 | Lets you fetch a number of items from an array. The function tests |
| 56 | 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] | 57 | 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] | 58 | the third parameter. |
| 59 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 60 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 61 | |
| 62 | $array = array( |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 63 | 'color' => 'red', |
| 64 | 'shape' => 'round', |
| 65 | 'radius' => '10', |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | 'diameter' => '20' |
| 67 | ); |
| 68 | |
| 69 | $my_shape = elements(array('color', 'shape', 'height'), $array); |
| 70 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 71 | The above will return the following array:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | |
| 73 | array( |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 74 | 'color' => 'red', |
| 75 | 'shape' => 'round', |
| 76 | 'height' => NULL |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | ); |
| 78 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 79 | You can set the third parameter to any default value you like. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | :: |
| 81 | |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 82 | $my_shape = elements(array('color', 'shape', 'height'), $array, 'foobar'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 84 | The above will return the following array:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 85 | |
| 86 | array( |
vlakoff | 0cd5535 | 2012-07-02 15:20:42 +0200 | [diff] [blame] | 87 | 'color' => 'red', |
| 88 | 'shape' => 'round', |
| 89 | 'height' => 'foobar' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 90 | ); |
| 91 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 92 | This is useful when sending the ``$_POST`` array to one of your Models. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 93 | This prevents users from sending additional POST data to be entered into |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 94 | your tables. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 95 | |
| 96 | :: |
| 97 | |
| 98 | $this->load->model('post_model'); |
| 99 | $this->post_model->update( |
| 100 | elements(array('id', 'title', 'content'), $_POST) |
| 101 | ); |
| 102 | |
| 103 | This ensures that only the id, title and content fields are sent to be |
| 104 | updated. |
| 105 | |
| 106 | random_element() |
| 107 | ================ |
| 108 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 109 | .. php:function:: random_element($array) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 111 | :param array $array: Input array |
| 112 | :returns: string (a random element from the 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 | Takes an array as input and returns a random element from it. |
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 | Usage example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 117 | |
| 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 Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 127 | echo random_element($quotes); |