Fatal error: Call to undefined function array_column()

4.77K views
no comments
13 Aug 2016 9:38 pm

Array column #php function added after release version of (PHP 5 >= 5.5.0, PHP 7), If error occurred please check the version of php by calling #php builtin function phpversion(); this function will works if you have php version greater than 5, If array_column function works on localhost and not defined on live hosting server so you ask them server’s team to upgrade the version #php.

If you are running shared hosting server or server team won’t allow you to upgrade the version of #php so I wrote a custom array_column function that will works, support array and object type data, so put the function code in your core project file and enjoy.

PHP array_column alternate function Array Column

if(!function_exists('array_column')){
	function array_column($array,$colname,$Indexkey=''){
		$return_array = array();
		if(is_array($array) || is_object($array)){
			foreach($array as $arrayDATA){
				if(is_object($arrayDATA)){
					if(isset($arrayDATA->{$colname})){
						if(isset($Indexkey) && isset($arrayDATA->{$Indexkey}) ){
							$return_array[$arrayDATA->{$Indexkey}] = $arrayDATA->{$colname};
						} else {
							$return_array[] = $arrayDATA->{$colname};
						}
					}
				} else if(is_array($arrayDATA)) {
					if(isset($arrayDATA[$colname])){
						if(isset($Indexkey) && isset($arrayDATA[$Indexkey]) ){
							$return_array[$arrayDATA[$Indexkey]] = $arrayDATA[$colname];	
						} else {
							$return_array[] = $arrayDATA[$colname];	
						}	
					}	
				}			
			}
		} 
		return $return_array;
	}
}

Here is custom Array Column function used

Check the result of function with array parameter and object parameter.

$array_coldata = array(
	0=> array('first_name'=>'Rameez','type'=>'person'), 
	1=> array('first_name'=>'Soomro','type'=>'caste') , 
	2=> array('first_name'=>'SDTuts','type'=>'site name'), 
	3=> array('first_name'=>'SDTuts.com','type'=>'domainname')  );
echo '


<pre>';
print_r(array_column($array_coldata,'first_name'));
echo '</pre>

';

Array(
[0] => Rameez
[1] => Soomro
[2] => SDTuts
[3] => SDTuts.com
)

$OBJ_coldata = json_decode(json_encode($array_coldata));
echo '


<pre>';
print_r(array_column($OBJ_coldata,'first_name'));
echo '</pre>

';

Array(
[0] => Rameez
[1] => Soomro
[2] => SDTuts
[3] => SDTuts.com
)

Here is the example of call array_column with index

The third parameter $indexkey is used to set value of passed indexkey in return array data.

print_r(array_column($array_coldata,'first_name','type'));

Result

Array
(
[person] => Rameez
[caste] => Soomro
[site name] => SDTuts
[domainname] => SDTuts.com
)

Here is another usage example copied from php array_column documentation page

class User {
    public $username;

    public function __construct($username)
    {
        $this->username = $username;
    }
}

$users = [
    new User('user 1'),
    new User('user 2'),
    new User('user 3'),
];

print_r(array_column($users, 'username'));

Result users name from class

Array
(
[0] => user 1
[1] => user 2
[2] => user 3
)

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>