Home > Enterprise >  Alternate of function array column
Alternate of function array column

Time:10-23

Can someone please help me to find this alternate code? I have an error it says "Cannot redeclare array_column()"

this is my code:

function array_column($array, $column_name) {
    $output = array();
    foreach($array as $keys => $values)
    {
        $output[] = $values[$column_name];
    }
    return $output;
}

thank you so much...

CodePudding user response:

If this is a polyfill so your code will work on old PHP versions that don't have array_column, you need to check if the function is defined first.

if (!function_exists('array_column')) {
    function array_column($array, $column_name) {
        $output = array();
        foreach($array as $keys => $values)
        {
            $output[] = $values[$column_name];
        }
        return $output;
    }
}

But if you've upgraded to a newer PHP version and don't have to be backward compatible, you can simply delete your function.

CodePudding user response:

There is a built-in function called 'array_column()' in php. You should give another name to your function. For example 'my_array_column()' or something like this.

  • Related