Array helper in CodeIgniter
×

Array helper in CodeIgniter

1349

Array helper contains functions that help us in working with arrays.


Related Topics:

Codeigniter Interview Questions
CodeIgniter Hooks
URL helper in CodeIgniter

Loading this Helper:

Use the given line in your controller or model file to load the array helper.

$this->load->helper('array');

The following functions are available in this helper:

1 element(): This function is used to get an element from an array. First of all this function check whether the array index is set and whether it has a value. If a value exists it is returned that value or NULL if the value does not exist.

Syntax:

element($item, $array[, $default = NULL]);

Parameter Description:

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

The return type of this function is mixed and it returns NULL on failure or the array item.

Example:

Step 1 Open the application/controllers directory and create a file Element_controller.php

<?php
class Element_controller extends CI_Controller {

public function index() {
$this->load->helper('array'); //load array helper
$array = array(
'name' => 'Ram',
'age' => '20',
'class' => ''
);

echo element('name', $array); // returns "Ram"
echo "<br>";
echo element('size', $array, '11th'); // returns "11th"
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Element_controller


2 elements(): This function is used to get one or more items from an array. This function checks whether each of the array indices is set or not. If an index does not exist it returns NULL or value set in the third parameter.

Syntax:

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

Parameter Description:

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

The return type of this function is mixed and it returns NULL on failure or the array item.

Example:

Step 1 Open the application/controllers directory and create a file Elements_controller.php

<?php
class Elements_controller extends CI_Controller {

public function index() {
$this->load->helper('array'); //load array helper
$array = array(
'name' => 'Ram',
'age' => '20',
'class' => '11th',
'branch' => 'Science'
);

$info=elements(array('name', 'age', 'branch','roll_number'), $array);
echo "<pre>";
print_r($info);
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Elements_controller


3 random_element(): This function is used to returns a random element from an array.

Syntax:

random_element($array);

Parameter Description:

  • $array (array) – Input array

The return type of this function is mixed and it returns a random element from the array.

Example:

Step 1 Open the application/controllers directory and create a file Random_e_controller.php

<?php
class Random_e_controller extends CI_Controller {

public function index() {
$this->load->helper('array'); //load array helper
$quotes = array(
"I find that the harder I work, the more luck I seem to have. - Thomas Jefferson",
"Don't stay in bed, unless you can make money in bed. - George Burns",
"We didn't lose the game; we just ran out of time. - Vince Lombardi",
"If everything seems under control, you're not going fast enough. - Mario Andretti",
"Reality is merely an illusion, albeit a very persistent one. - Albert Einstein",
"Chance favors the prepared mind - Louis Pasteur"
);

echo random_element($quotes);
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Random_e_controller



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments