php 替代 array_column()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27422640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternate to array_column()
提问by Sizzling Code
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.
我array_column()
在一个项目中使用过,上传后发现只有PHP 5.5以上才支持这个功能,而且我觉得我用的主机不支持PHP 5.5以上。
So I want to know if is there any alternate to fix this error?
所以我想知道是否有任何替代方法可以解决此错误?
This is how I am using array_count
in my project:
这是我array_count
在我的项目中使用的方式:
array_count_values(array_column(json_decode(json_encode($queryResultArray), true), $idForBar));
This is working fine in my local xampp and wampp also, but on server it is giving issue. Looking any alternate function or solution.
这在我的本地 xampp 和 wampp 中也运行良好,但在服务器上却出现了问题。寻找任何替代功能或解决方案。
回答by Pupil
Add your own function array_column
if you PHP version does not support it:
array_column
如果您的 PHP 版本不支持,请添加您自己的函数:
<?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;
}
}
回答by Legionar
You can also use array_map()
function if you haven't array_column()
because of PHP<5.5:
array_map()
如果你没有array_column()
因为 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!
以上适用于 PHP 5.3.0 及更高版本!
If you have < PHP 5.3.0, as you wrote PHP 5.2.17, just use simple function:
如果你有 < PHP 5.3.0,就像你写 PHP 5.2.17 一样,只需使用简单的函数:
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);
回答by Codebrekers
You can also use the alternative code of array_column(), it's simple just paste below line and replace your variable.
您也可以使用 array_column() 的替代代码,只需将其粘贴到行下方并替换您的变量即可。
Code:
代码:
array_map(function($element){return $element['last_name'];}, $a);
回答by Wajeel
You can always use another implementation of function array_column
您始终可以使用函数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;
}
}
回答by Mark Baker
Using array_map()instead, something like:
使用array_map()代替,例如:
array_count_values(
array_map(
function($value) use ($idForBar) {
return $value[$idForBar];
},
json_decode(
json_encode($queryResultArray),
true
)
)
);
回答by doub1eHyman
There is an official recommendationfor PHP versions that don't support array_colum()
under the "see also" section:
对于不支持的 PHP 版本,“另请参阅”部分有官方建议array_colum()
:
? 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.
他们推荐的是另一种if (!function_exists('array_column'))
方法,但代码实际上是从 array_column 库中提取的,并且比本页上的示例更通用。