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:

  • Predefined functions

The following function which is used in array helper.

Syntax:

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:

Use of element() function in array helper:

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

Syntax:

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

Tests.php

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:

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

Tests.php

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:

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

Tests.php

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.

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

Pin It on Pinterest

Share This