CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array.

  • Load Array helper

It is used to load the helper class. We can pass the helper function in the controller’s method.

Syntax:

 $this->load->helper(‘array’);
  • Predefined functions

The following function which is used in array helper.

Syntax:

element ($item, $array, $default_value);

Parameters: 

  • $item(string): It fetches data item form the array.
  • $array: It contains all the input data item.
  • $default_value: If the array is empty, it returns the default value that we set.

Suppose there is an array that contains all the data items and you want to fetch an item from an array.The element() functionchecks for proper array index and value.

  • If a value exists in the array, it returns array element.
  • If the value does not exist, it returns NULL or the default value, which you have set in the array element.

Syntax:

            $arr = array(
                                     ‘xyz’ => ‘red’,
                                     ‘name’ => ‘blue’,
                                     );
             echo element( ‘xyz’ , $arr);  // this is used to fetch the particular item “red”
             echo element(‘ topic’ , $arr, ‘PHP’); // this will return default value “PHP” 

Use of element() function in array helper:

An element() function is used to fetch a single data item from an array.

Syntax:

echo element('email', $employee);

Example: Create a Tests.php file in application/controller folder and write the following program:

Tests.php

<?php
 class Tests extends CI_Controller {
     public function array_class()     // it is a method that call on the server
     {  
          echo "<title> Tutorials and examples </title>";
          $this->load->helper('array');         // load the array helper
         $employee = array (     
             'name' => 'Herry',
             'email' => '[email protected]',
             'emp_id' => '101',  
         );     
     echo element(‘email’, $employee); // for fetching particular item from the employee array
         }
 } 
 ?> 

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/test/array_class. It returns a single element from the array.

Output:

CodeIgniter Array Helper

Use of elements function in array helper:

An elements() function is used to fetch multiple data items from an array.

Syntax:

$data = elements (array (‘xyz, ‘name’), $arr); 

Example: Create a Tests.php file in application/controller folder and write the following program:

Tests.php

<?php
 class Tests extends CI_Controller {
     public function array_class()     
     {   
          echo "<title> Tutorials and examples </title>";
          $this->load->helper('array');         // load the array helper
         $employee = array(    
             'name' => 'Herry',
             'email' => '[email protected]', 
             'emp_id' => '101',     
         );
       $data = elements (array (‘name', 'email', 'emp_id'), $employee); 
     print_r($data); // for fetching multiple items from the employee array
         }
 }
 ?> 

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/test/array_class. It returns multiple data elements from an array.

Output:

CodeIgniter Array Helper

Use of Default value

The default value is used whenever the particular key’s value is not present; then, it will return either null or the value which you have passed at the run time in an array.

Syntax:

 elements (array
(‘xyz, ‘name’, ‘email’), $arr, ‘[email protected]’); 

Example: Create a Tests.php file in application/controller folder and write the following program:

Tests.php

<?php
 class Tests extends CI_Controller {
     public function array_class()     
     {  
          echo "<title> Tutorials and examples </title>";
          $this->load->helper('array');         
         $employee = array(     
             'name' => 'Herry',
             'email' => '[email protected]',
             'emp_id' => '101',     
         ); 
   $data= elements (array (‘name’, 'email', ‘emp_id’, ‘job’), $employee, 'herry@456');
     print_r($data); 
         }
 }
 ?> 

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/test/array_class. It returns all the elements along with additional data.

Output:

CodeIgniter Array Helper

random_element() Function

The random_element() function is used to fetch data from an array, randomly. For this, you have to define the statements in an array and call the random_element() function. So, when you run the program in a local server, it shows the different result on the screen.  When you refresh the page, it shows the different results.

Example:  Create a Tests.php file in application/controller folder and write the following program. In the array we have defined three statements.

<?php
 class Tests extends CI_Controller {
     public function array_class()
     {   echo "<title> Tutorials and examples </title>";
          $this->load->helper('array');        
         $tutorial = array (   // define number of statement which calls randomly 
             "CodeIgniter is a PHP framework",
             "CodeIgniter is easy to learn and develop application",
             "CodeIgniter is a light weight application" 
         ); 
         echo random_element($tutorial);
     } 
 } 

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/test/array_class, it randomly calls a statement that you have defined in an array.

Output:

CodeIgniter Array Helper

After refreshing the page, it shows the following output.

CodeIgniter Array Helper