From 3a082fd3d6cb2e72612d51b9d8e54e93effb93eb Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Thu, 7 Oct 2010 09:38:55 -0500 Subject: added elements() to the Array Helper to return elements from an array with specified keys only. Differs from array_intersect_assoc() in that a default value can be provided for keys that do not exist in the supplied array --- user_guide/helpers/array_helper.html | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'user_guide/helpers/array_helper.html') diff --git a/user_guide/helpers/array_helper.html b/user_guide/helpers/array_helper.html index c0bdd78e3..6d95c4a5f 100644 --- a/user_guide/helpers/array_helper.html +++ b/user_guide/helpers/array_helper.html @@ -100,7 +100,58 @@ echo element('size', $array, NULL); echo random_element($quotes); +

elements()

+

Lets you fetch a number of items from an array. The function tests whether each of the array indices is set. If an index does not exist +it is set to FALSE, or whatever you've specified as the default value via the third parameter. Example:

+ + +$array = array(
+    'color' => 'red',
+    'shape' => 'round',
+    'radius' => '10',
+    'diameter' => '20'
+);
+
+$my_shape = elements(array('color', 'shape', 'height'), $array);
+
+ +

The above will return the following array:

+ + +array(
+    'color' => 'red',
+    'shape' => 'round',
+    'height' => FALSE
+); +
+ +

You can set the third parameter to any default value you like:

+ + +$my_shape = elements(array('color', 'shape', 'height'), $array, NULL);
+
+ +

The above will return the following array:

+ + +array(
+    'color' => 'red',
+    'shape' => 'round',
+    'height' => NULL
+); +
+ +

This is useful when sending the $_POST array to one of your Models. This prevents users from +sending additional POST data to be entered into your tables:

+ + +$this->load->model('post_model');
+
+$this->post_model->update(elements(array('id', 'title', 'content'), $_POST)); +
+ +

This ensures that only the id, title and content fields are sent to be updated.

-- cgit v1.2.3-24-g4f1b