Home » Php » php – Alternate to array_column()

php – Alternate to array_column()

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have used array_column() 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 array_count 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.

How to&Answers:

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

<?php
if (! function_exists('array_column')) {
    function array_column(array $input, $columnKey, $indexKey = null) {
        $array = array();
        foreach ($input as $value) {
            if ( !array_key_exists($columnKey, $value)) {
                trigger_error("Key \"$columnKey\" does not exist in array");
                return false;
            }
            if (is_null($indexKey)) {
                $array[] = $value[$columnKey];
            }
            else {
                if ( !array_key_exists($indexKey, $value)) {
                    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;
    }
}

Reference:

Answer:

You can also use array_map() function if you haven’t array_column() because of PHP<5.5:

Example:

$a = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    )
);

array_column($a, 'last_name');

Becomes:

array_map(function($element) {
  return $element['last_name'];
}, $a);

So it your case the code will be:

array_count_values(
  array_map(function($arr) use ($idForBar) {
    return $arr[$idForBar];
  }, $queryResultArray)
);

This above is working on PHP 5.3.0 and above!

If you have < PHP 5.3.0, as you wrote PHP 5.2.17, just use simple function:

function get_field_data($array, $field, $idField = null) {
    $_out = array();

    if (is_array($array)) {
        if ($idField == null) {
            foreach ($array as $value) {
                $_out[] = $value[$field];
            }
        }
        else {
            foreach ($array as $value) {
                $_out[$value[$idField]] = $value[$field];
            }
        }
        return $_out;
    }
    else {
        return false;
    }           
}

And the usage:

$output = get_field_data($queryResultArray, $idForBar);

Answer:

You can also use the alternative code of array_column(), it’s simple just paste below line and replace your variable.

Code:

array_map(function($element){return $element['last_name'];}, $a);

Answer:

Using array_map() instead, something like:

array_count_values(
    array_map(
        function($value) use ($idForBar) {
            return $value[$idForBar];
        },
        json_decode(
            json_encode($queryResultArray),
            true
        )
    )
);

Answer:

You can always use another implementation of function array_column

if (!function_exists('array_column')) {
    function array_column(array $array, $columnKey, $indexKey = null)
    {
        $result = array();
        foreach ($array as $subArray) {
            if (!is_array($subArray)) {
                continue;
            } elseif (is_null($indexKey) && array_key_exists($columnKey, $subArray)) {
                $result[] = $subArray[$columnKey];
            } elseif (array_key_exists($indexKey, $subArray)) {
                if (is_null($columnKey)) {
                    $result[$subArray[$indexKey]] = $subArray;
                } elseif (array_key_exists($columnKey, $subArray)) {
                    $result[$subArray[$indexKey]] = $subArray[$columnKey];
                }
            }
        }
        return $result;
    }
}

Answer:

There is an official recommendation for PHP versions that don’t support array_colum() under the “see also” section:

» Recommended userland implementation for PHP lower than 5.5

Their recommendation is another if (!function_exists('array_column')) approach, but the code is actually extracted from the array_column library and is a little more generalized than the examples on this page.