Alternate to array_column()

前端 未结 6 1426
我寻月下人不归
我寻月下人不归 2020-12-02 18:57

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

相关标签:
6条回答
  • 2020-12-02 19:09

    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);
    
    0 讨论(0)
  • 2020-12-02 19:12

    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.

    0 讨论(0)
  • 2020-12-02 19:14

    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:

    0 讨论(0)
  • 2020-12-02 19:16

    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);
    
    0 讨论(0)
  • 2020-12-02 19:16

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-02 19:36

    Using array_map() instead, something like:

    array_count_values(
        array_map(
            function($value) use ($idForBar) {
                return $value[$idForBar];
            },
            json_decode(
                json_encode($queryResultArray),
                true
            )
        )
    );
    
    0 讨论(0)
提交回复
热议问题