简体   繁体   English

在PHP5中可以在PHP7中工作的代码有哪些替代方法

[英]What alternative of code that work in PHP7 in PHP5

I have a PHP code which runs on PHP7 but not in PHP 5, which PHP version in my server, that is my code: 我有一个PHP代码可以在PHP7上运行,但不能在PHP 5上运行,该服务器上的PHP版本是我的代码:

Array (
    [0] => stdClass Object (
        [userId] => 15
        [name] => name0
        [userName] => hh
        [centerName] => center10
    )

    [1] => stdClass Object (
        [userId] => 16
        [name] => name1
        [userName] => test
        [centerName] => center10
    )

    [2] => stdClass Object (
        [userId] => 1
        [name] => name2
        [userName] => ll
        [centerName] => center1
    )
)

$ids = array_unique(array_column($results, 'centerName'));
print_r($ids);

I get what I expected in PHP7 but I get an empty array in PHP5. 我得到了PHP7中的期望,但是我得到了PHP5中的空数组。

How can I adapt my code to work in PHP5? 如何修改我的代码以在PHP5中工作?

As from manual , ability to use array of objects as parameter to array_column is added since php7.0 . 手册开始 ,自php7.0起增加了使用对象数组作为array_column参数的功能

In php5 you simply cannot use array_column to get columns from array of objects. 在php5中,您根本无法使用array_column从对象数组中获取列。 So, you need to use some other code, a simple foreach for example: 因此,您需要使用其他一些代码,例如一个简单的foreach

$uniqueValues = [];
foreach ($results as $item) {
    $uniqueValues[$item->centerName] = 1;
}
print_r(array_keys($uniqueValues));

In the manual to array_column you can get code how to use array_column legacy code on older versions of PHP. 在array_column手册中,您可以获得在旧版本的PHP上如何使用array_column旧代码的代码
I like this method as it also incorporate the third parameter. 我喜欢这种方法,因为它还包含了第三个参数。

if (!function_exists('array_column')) {
    function array_column($array, $columnKey, $indexKey = null)
    {
        $result = array();
        foreach ($array as $subArray) {
            if (is_null($indexKey) && array_key_exists($columnKey, $subArray)) {
                $result[] = is_object($subArray)?$subArray->$columnKey: $subArray[$columnKey];
            } elseif (array_key_exists($indexKey, $subArray)) {
                if (is_null($columnKey)) {
                    $index = is_object($subArray)?$subArray->$indexKey: $subArray[$indexKey];
                    $result[$index] = $subArray;
                } elseif (array_key_exists($columnKey, $subArray)) {
                    $index = is_object($subArray)?$subArray->$indexKey: $subArray[$indexKey];
                    $result[$index] = is_object($subArray)?$subArray->$columnKey: $subArray[$columnKey];
                }
            }
        }
        return $result;
    }
}

And in your case the code would be the above and: 在您的情况下,代码将是上面的代码:

$ids = array_keys(array_column($results, 'name' ,'centerName'));
print_r($ids);

This puts the centername as keys and name as value meaning it will remove all duplicates automatically, then just grab the keys. 这会将centername作为键,并将name作为值,这意味着它将自动删除所有重复项,然后只需抓住键即可。

Array_column is a great function to have in your project. Array_column是一个很棒的函数,可以在您的项目中使用。

I thought I'd offer another solution, despite the solutions provided by @u_mulder and @Andreas being great answers. 我以为我会提供另一个解决方案,尽管@u_mulder@Andreas提供的解决方案很好。

function new_array_column($input , $column_key, $index_key = null) {
    $result = array();
    array_walk($input, function($value, $key) use (&$result, $column_key, $index_key) {
        $result[empty($index_key) ? $key : is_object($value) ? $value->{$index_key} : $value[$index_key]]
        = empty($column_key) ? $value : (is_object($value) ? $value->{$column_key} : $value[$column_key]);
    });
    return $result;
}

This accepts all the parameters that the new array_column does, but is kept nice and concise by using the array_walk . 这接受了新array_column所有参数,但是通过使用array_walk保持简洁明了。

The code looks horrible but it is fairly straight forward, but here is a brief description: 该代码看起来很可怕,但是很简单,但是这里是一个简短的描述:

  1. Determine whether an $index_key was passed, if it was then set that key now, otherwise use the original key. 确定是否传递了$index_key (如果现在已将该键设置),否则,请使用原始键。
  2. Check whether a $column_key was specified. 检查是否指定了$column_key
    2.1. 2.1。 If no $column_key is passed, then use the entire value. 如果没有传递$column_key ,则使用整个值。
    2.2. 2.2。 Otherwise, use the value from that column. 否则,请使用该列中的值。

I have tested this on PHP 5.3.0 , which is the earliest version this code will work on. 我已经在PHP 5.3.0上对此进行了测试,这是此代码可以使用的最早版本。 (The earlier version don't accept an anonymous function in the array_walk function, it must be passed as a string). (较早的版本在array_walk函数中不接受匿名函数,它必须作为字符串传递)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM