替代 array_column() [英] Alternate to array_column()

查看:21
本文介绍了替代 array_column()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中使用过array_column(),上传后发现只有PHP 5.5以上才支持这个功能,我觉得我用的主机不支持PHP 5.5或者

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?

这是我在项目中使用 array_count 的方式:

This is how I am using array_count in my project:

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

这在我的本地 xampp 和 wampp 中也能正常工作,但在服务器上却出现问题.寻找任何替代功能或解决方案.

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

推荐答案

添加自己的函数array_column 如果你的 PHP 版本不支持它:

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;
    }
}

参考:

这篇关于替代 array_column()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆