How to convert array values to lowercase in PHP?

前端 未结 10 755
北恋
北恋 2020-11-30 19:26

How can I convert all values in an array to lowercase in PHP?

Something like array_change_key_case?

相关标签:
10条回答
  • 2020-11-30 20:16

    If you wish to lowercase all values in an nested array, 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']   
    
    0 讨论(0)
  • 2020-11-30 20:16

    You can also use a combination of array_flip() and array_change_key_case(). See this post

    0 讨论(0)
  • 2020-11-30 20:18

    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.

    // 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);
    
    0 讨论(0)
  • 2020-11-30 20:20

    `$Color = array('A' => 'Blue', 'B' => 'Green', 'c' => 'Red');

    $strtolower = array_map('strtolower', $Color);

    $strtoupper = array_map('strtoupper', $Color);

    print_r($strtolower); print_r($strtoupper);`

    0 讨论(0)
提交回复
热议问题