如何在 PHP 中将数组值转换为小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11008443/
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
How to convert array values to lowercase in PHP?
提问by user1163513
How can I convert all values in an array to lowercase in PHP?
如何在 PHP 中将数组中的所有值转换为小写?
Something like array_change_key_case?
像array_change_key_case什么?
回答by ariefbayu
use array_map():
使用array_map():
$yourArray = array_map('strtolower', $yourArray);
In case you need to lowercase nested array(by Yahya Uddin):
如果您需要小写嵌套数组(由Yahya Uddin 提供):
$yourArray = array_map('nestedLowercase', $yourArray);
function nestedLowercase($value) {
if (is_array($value)) {
return array_map('nestedLowercase', $value);
}
return strtolower($value);
}
回答by Alex Shesterov
Just for completeness: you may also use array_walk:
只是为了完整性:您也可以使用array_walk:
array_walk($yourArray, function(&$value)
{
$value = strtolower($value);
});
From PHP docs:
来自 PHP 文档:
If callback needs to be working with the actual values of the array, specify the first parameter of callback as a reference. Then, any changes made to those elements will be made in the original array itself.
如果回调需要处理数组的实际值,请指定回调的第一个参数作为引用。然后,对这些元素所做的任何更改都将在原始数组本身中进行。
Or directly via foreachloopusing references:
foreach($yourArray as &$value)
$value = strtolower($value);
Note that these two methods change the array "in place", whereas array_mapcreates and returns a copy of the array, which may not be desirable in case of very large arrays.
请注意,这两种方法“就地”更改数组,而array_map创建并返回数组的副本,这在非常大的数组的情况下可能是不可取的。
回答by verisimilitude
You could use array_map(), set the first parameter to 'strtolower' (including the quotes) and the second parameter to $lower_case_array.
您可以使用array_map(),将第一个参数设置为'strtolower'(包括引号),将第二个参数设置为$lower_case_array。
回答by Yahya Uddin
If you wish to lowercase all values in an nestedarray, use the following code:
如果您希望小写嵌套数组中的所有值,请使用以下代码:
function nestedLowercase($value) {
if (is_array($value)) {
return array_map('nestedLowercase', $value);
}
return strtolower($value);
}
So:
所以:
[ 'A', 'B', ['C-1', 'C-2'], 'D']
would return:
会返回:
[ 'a', 'b', ['c-1', 'c-2'], 'd']
回答by ???????? ?????
array_change_value_case
array_change_value_case
by continue
通过继续
function array_change_value_case($array, $case = CASE_LOWER){
if ( ! is_array($array)) return false;
foreach ($array as $key => &$value){
if (is_array($value))
call_user_func_array(__function__, array (&$value, $case ) ) ;
else
$array[$key] = ($case == CASE_UPPER )
? strtoupper($array[$key])
: strtolower($array[$key]);
}
return $array;
}
$arrays = array ( 1 => 'ONE', 2=> 'TWO', 3 => 'THREE',
'FOUR' => array ('a' => 'Ahmed', 'b' => 'basem',
'c' => 'Continue'),
5=> 'FIVE',
array('AbCdeF'));
$change_case = array_change_value_case($arrays, CASE_UPPER);
echo "<pre>";
print_r($change_case);
Array ( [1] => one [2] => two [3] => three [FOUR] => Array ( [a] => ahmed [b] => basem [c] => continue ) [5] => five [6] => Array ( [0] => abcdef ) )
Array ( [1] => one [2] => two [3] => three [FOUR] => Array ( [a] => ahmed [b] => basem [c] => continue ) [5] => five [6] => Array ( [0] => abcdef ) )
回答by Jasmeen
array_map()is the correct method. But, if you want to convert specific array values or all array values to lowercase one by one, you can use strtolower().
array_map()是正确的方法。但是,如果要将特定数组值或所有数组值一一转换为小写,则可以使用strtolower().
for($i=0; $i < count($array1); $i++) {
$array1[$i] = strtolower($array1[$i]);
}
回答by Junaid Atari
AIO Solution / Recursive / Unicode|UTF-8|Multibyte supported!
AIO 解决方案 / 递归 / Unicode|UTF-8|支持多字节!
/**
* Change array values case recursively (supports utf8/multibyte)
* @param array $array The array
* @param int $case Case to transform (\CASE_LOWER | \CASE_UPPER)
* @return array Final array
*/
function changeValuesCase ( array $array, $case = \CASE_LOWER ) : array {
if ( !\is_array ($array) ) {
return [];
}
/** @var integer $theCase */
$theCase = ($case === \CASE_LOWER)
? \MB_CASE_LOWER
: \MB_CASE_UPPER;
foreach ( $array as $key => $value ) {
$array[$key] = \is_array ($value)
? changeValuesCase ($value, $case)
: \mb_convert_case($array[$key], $theCase, 'UTF-8');
}
return $array;
}
Example:
例子:
$food = [
'meat' => ['chicken', 'fish'],
'vegetables' => [
'leafy' => ['collard greens', 'kale', 'chard', 'spinach', 'lettuce'],
'root' => ['radish', 'turnip', 'potato', 'beet'],
'other' => ['brocolli', 'green beans', 'corn', 'tomatoes'],
],
'grains' => ['wheat', 'rice', 'oats'],
];
$newArray = changeValuesCase ($food, \CASE_UPPER);
Output
输出
[
'meat' => [
0 => 'CHICKEN'
1 => 'FISH'
]
'vegetables' => [
'leafy' => [
0 => 'COLLARD GREENS'
1 => 'KALE'
2 => 'CHARD'
3 => 'SPINACH'
4 => 'LETTUCE'
]
'root' => [
0 => 'RADISH'
1 => 'TURNIP'
2 => 'POTATO'
3 => 'BEET'
]
'other' => [
0 => 'BROCOLLI'
1 => 'GREEN BEANS'
2 => 'CORN'
3 => 'TOMATOES'
]
]
'grains' => [
0 => 'WHEAT'
1 => 'RICE'
2 => 'OATS'
]
]
回答by Gregory Bologna
You don't say if your array is multi-dimensional. If it is, array_map will not work alone. You need a callback method. For multi-dimensional arrays, try array_change_key_case.
你不会说你的数组是否是多维的。如果是,array_map 将无法单独工作。你需要一个回调方法。对于多维数组,请尝试array_change_key_case。
// You can pass array_change_key_case a multi-dimensional array,
// or call a method that returns one
$my_array = array_change_key_case(aMethodThatReturnsMultiDimArray(), CASE_UPPER);
回答by hasnath rumman
`$Color = array('A' => 'Blue', 'B' => 'Green', 'c' => 'Red');
`$Color = array('A' => '蓝色', 'B' => '绿色', 'c' => '红色');
$strtolower = array_map('strtolower', $Color);
$strtolower = array_map('strtolower', $Color);
$strtoupper = array_map('strtoupper', $Color);
$strtoupper = array_map('strtoupper', $Color);
print_r($strtolower); print_r($strtoupper);`
打印_r($strtolower); print_r($strtoupper);`

