Sizzling Code Sizzling Code - 1 year ago 149
PHP Question

Alternate to array_column()

I have used

in a project, and after uploading I found out that only PHP 5.5 or above support this function, and I think the hosting I use don't support PHP 5.5 or above.

So I want to know if is there any alternate to fix this error?

This is how I am using
in my project:

array_count_values(array_column(json_decode(json_encode($queryResultArray), true), $idForBar));

This is working fine in my local xampp and wampp also, but on server it is giving issue. Looking any alternate function or solution.

Answer Source

Add your own function array_column if you PHP version does not support it:

if (! function_exists('array_column')) {
    function array_column(array $input, $columnKey, $indexKey = null) {
        $array = array();
        foreach ($input as $value) {
            if ( ! isset($value[$columnKey])) {
                trigger_error("Key \"$columnKey\" does not exist in array");
                return false;
            if (is_null($indexKey)) {
                $array[] = $value[$columnKey];
            else {
                if ( ! isset($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not exist in array");
                    return false;
                if ( ! is_scalar($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not contain scalar value");
                    return false;
                $array[$value[$indexKey]] = $value[$columnKey];
        return $array;


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download