How to convert array values to lowercase in PHP?

前端 未结 10 796
北恋
北恋 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:03

    Just for completeness: you may also use array_walk:

    array_walk($yourArray, function(&$value)
    {
      $value = strtolower($value);
    });
    

    From PHP docs:

    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 foreach loop using references:

    foreach($yourArray as &$value)
      $value = strtolower($value);
    

    Note that these two methods change the array "in place", whereas array_map creates and returns a copy of the array, which may not be desirable in case of very large arrays.

提交回复
热议问题