×

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 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.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 minutes read.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

6 minutes read.

CodeIgniter Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to...

3 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 CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

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.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 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 Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.