Alternate to array_column as not working in PHP 5.3

In this article, we going to see array_column not working in PHP 5.3. We going to use an alternative of array function called array_column.

Using alternative of array_column function we going to achieve same result as we are getting from array_column.

We going to use array_map() function if you haven’t array_column() because of PHP<5.5, the functionality array map is merely the same as that array column, so we can use array map to achieve the result as that we are getting from using array column.

As we know that array_column() function returns the values from a single column in the input array, so alternatively using array map we going to get result.

Lets Start with point Alternate to array_column as not working in PHP 5.3, going to check step by step:

So Let’s with this, I have an array which contain the employees details with employeid, name , amount.

These employee details are in the form called a multidimensional array. From this array, I have to find the specific value of employees. For this, I am going using to array function.

Similar result we going to achieve using array map.

The array function i am going to use are as follow as:

- array_column()
- array_map()

So how we going to use this both array function to check string or value is contain in multidimensional array.Suppose i want employ name for all employ. That is i want to check employ id is present in array or not so i am going to use first array_column.

Array column what going to do is returns the values from a single column in the input array. So for that, I have to pass the array key. After passing the array key it will return a single column. Finally, we get the result using an array column, similar we get the result using an array map.

Below Is the example how we can achieve this.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>contains demo</title>
</head>
<body>
<?php
 
$search=Array
        (
        '0' => Array
           (
           'emp_id' => '100',
           'emp_name' => 'Peter',
           'emp_amount' => '900'
           ),
        '1' => Array
           (
           'emp_id' => '101',
           'emp_name' => 'John',
           'emp_amount' => '800'
           ),
        '2' => Array
           (
           'emp_id' => '106',
           'emp_name' => 'Jack',
           'emp_amount' => '500'
           )
        );
		
		$a = array_column($search, 'emp_name');
		echo 'Using Array-Column (Printed array value):<br/>';
		print_r($a);
		echo '<br/>';
		
		echo '<br/>Using Array-Map :';
		array_map(function($element) {
		echo  "<br/>".$element['emp_name'];
		}, $search);
 
?>
</body>
</html>

OUTPUT :

Output of above example
Output of above example

What is PHP array_column() function?

PHP array_column() function use for returns the values from a single column in the input array.PHP array_column() syntax is array_column(array, column_key, index_key).In which parameter array specifies the multi-dimensional array to use, parameter column_key an integer key or a string key name of the column of values to return, parameter index_key optional.

What is PHP array_map() function?

PHP array_map() the function sends each value of an array to a user-made function and returns an array with new values, given by the user-made function. And the syntax for array_map is array_map(myfunction, array1, array2, array3, …).

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.

Leave a Comment