×

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' => 'herry@gmail.com',
             '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' => 'herry@gmail.com', 
             '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, ‘abc@yahoo.com’); 

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' => 'herry@gmail.com',
             '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

Related Topics

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

Update a Record CodeIgniter

Update a record In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set()...

10 minutes read.

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes read.

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

3 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.