elements()

elements($items, $array[, $default = NULL])

Parameters:
  • $item (string) – Item to fetch from the array
  • $array (array) – Input array
  • $default (bool) – What to return if the array isn’t valid
Returns:

NULL on failure or the array item.

Return type:

mixed

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 NULL, 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' => NULL
);

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

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

The above will return the following array:

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

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.

doc_CodeIgniter
2016-10-15 16:32:16
Comments
Leave a Comment

Please login to continue.