候补array_column() [英] Alternate to array_column()

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

问题描述

我用 array_column()在一个项目中,并上传后,我发现只有PHP 5.5或以上版本支持此功能,我想主办我用唐'T支持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 ( ! 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;
    }
}

参考:

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

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