- array_change_key_case
- Parameters
- Return Values
- Examples
- Notes
- PHP RFC: array_change_keys()
- The Problem
- Proposal
- Callback
- Returning Invalid Types
- Returning Duplicate Keys
- Function Name
- Discussion
- Pros
- Provides Common General-Purpose Functionality
- Usefulness
- Cleaner Code
- Matches Existing Behavior
- Faster Execution Than array_combine()
- Works With Functional Code
- Cons
- Slower Than foreach
- Does Not Support Traversable
- Easily Implemented In User Land
- how to change array keys from uppercase to lowercase?
array_change_key_case
Returns an array with all keys from array lowercased or uppercased. Numbered indices are left as is.
Parameters
Either CASE_UPPER or CASE_LOWER (default)
Return Values
Returns an array with its keys lower or uppercased, or null if array is not an array.
Examples
Example #1 array_change_key_case() example
$input_array = array( «FirSt» => 1 , «SecOnd» => 4 );
print_r ( array_change_key_case ( $input_array , CASE_UPPER ));
?>?php
The above example will output:
Array ( [FIRST] => 1 [SECOND] => 4 )
Notes
Note:
If an array has indices that will be the same once run through this function (e.g. » keY » and » kEY «), the value that is later in the array will override other indices.
- Array Functions
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_is_list
- array_key_exists
- array_key_first
- array_key_last
- array_keys
- array_map
- array_merge_recursive
- array_merge
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace_recursive
- array_replace
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff_assoc
- array_udiff_uassoc
- array_udiff
- array_uintersect_assoc
- array_uintersect_uassoc
- array_uintersect
- array_unique
- array_unshift
- array_values
- array_walk_recursive
- array_walk
- array
- arsort
- asort
- compact
- count
- current
- end
- extract
- in_array
- key_exists
- key
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort
- each
PHP RFC: array_change_keys()
This RFC proposes a new core function to simplify the process of changing an array’s keys (“re-keying”).
The Problem
PHP only has one existing function to change an array’s keys: array_change_key_case() . Unfortunately this can only transform keys to upper- or lower-case.
Because this function doesn’t allow developers to specify their own custom logic to transform keys to something else, one must typically resort to using a foreach loop to build a new array:
$newArray = []; foreach ($oldArray as $key => $value) { $newKey = 'someValue'; // Whatever custom logic is needed $newArray[$newKey] = $value; }
This approach requires 4 lines of code. Furthermore, this logic cannot be wrapped as a parameter to some other function.
That latter issue can solved by composing several existing functions like so:
$newArray = array_combine( array_map( function ($key, $value) { return 'someValue'; // Whatever custom logic is needed }, array_keys($oldArray), $oldArray ), $oldArray );
While this does work, it has some major drawbacks:
If you want to re-key an iterator’s results, you cannot do so inline — a temporary variable would be needed to convert the iterator to array first.
Proposal
This RFC proposes the creation of a new core function array_change_keys() :
$newArray = array_change_keys($originalArray, function ($key, $value) { return 'someValue'; // Whatever custom logic is needed });
array array_change_keys(array $originalArray, callable $callback)
This function takes two arguments:
(This parameter order is consistent with all other array functions except for array_map() , which is a special case due to its variadic nature.)
A new array will be returned from this function, containing the same values in the same order but with potentially different keys. Some values may not be included if an invalid or duplicate key is returned by the callback. This behavior is identical to array_flip() and is documented in the “Callback” subsections further below.
Callback
Two parameters will be passed to the callable for each element in the array:
The callable must return a string or int to be used as the new key.
Returning Invalid Types
The callable must return a valid key. Returning any type besides string or int will result in the following warning:
Warning: array_change_keys(): New key should be either a string or an integer
Additionally, the current array item will not be added to the resulting array. PHP will still attempt to process all subsequent elements.
This matches the behavior of calling array_flip on an array containing types other than string or int .
Returning Duplicate Keys
If the callable returns the same key for multiple values, the last occurrence “wins” and all prior values will be lost. For example:
var_dump(array_change_keys([1, 2, 3], function(){ return 'foo'; })); // array(1) // ["foo"]=> // int(3) // >
This behavior also matches array_flip() .
Function Name
PHP already has an array_change_key_case() function, so sharing a common root name ( array_change_key ) seems like a logical choice.
Other functions which deal with multiple keys (like array_keys and array_fill_keys ) are pluralized, so we’re using that same convention here.
Discussion
This section will be updated with any additional pros/cons that arise during the discussion period.
Pros
Provides Common General-Purpose Functionality
PHP already has an array_change_key_case function, which is an incredibly specific implementation that isn’t useful in the majority of cases where an array needs to be re-keyed. By providing a general-purpose function for a common problem we prevent the need for other array_change_key_* variants in the future.
Usefulness
Needing to re-key array is a common task for some PHP developers, especially those needing their array to work like a dictionary.
Cleaner Code
Using this function makes it immediately obvious to other developers that an array is being re-keyed.
Matches Existing Behavior
The “edge cases” mentioned above (returning invalid types or duplicate keys) matches existing behavior in PHP that developers already understand and expect. No new edge cases or quirks are being introduced with this RFC .
Faster Execution Than array_combine()
Re-keying an array with array_change_keys() is faster than the array_combine approach:
Works With Functional Code
This function can be nested inside of other method calls for function composition. The same is not possible for the foreach approach (without requiring the creation of a separate method to encapsulate that functionality).
Cons
Slower Than foreach
As noted in the benchmarks above, the foreach loop approach is faster than array_change_keys in most (but not all) cases.
Does Not Support Traversable
Like the other array_ functions, this one also doesn’t support iterators, which may be seen as a step backwards.
Easily Implemented In User Land
This function can be implemented in user land using one of the alternative approaches shown above. There’s a general feeling among some developers that “what can be implemented in userland shouldn’t be in core”.
how to change array keys from uppercase to lowercase?
But it is not working This is my array, it also contains objects, it is a fetch from a database, sry for printing it all Array ( [0] => stdClass Object ( [ID] => 4 [TUTOR_ID] => 4 [PRICE] => 25 [TITLE] => Introduction Mathematical Analysis [FEATURED] => 1 [DESCRIPTION] => This course provides the user with a smooth introduction in the field of Mathematical Analysis. [DATE_ADDED] => 0 [rating] => 2 [subscribers] => 18 [categories] => Array ( ) [languages] => Array ( ) [chapters] => Array ( [0] => stdClass Object ( [ID] => 49 [COURSE_ID] => 4 [START_TIME] => 3977118 [END_TIME] => 3982720 [DURATION] => 3600 [TITLE] => Conclusions & Questioning [DESCRIPTION] => Wrap-up and Discussions. ) [1] => stdClass Object ( [ID] => 48 [COURSE_ID] => 4 [START_TIME] => 3821895 [END_TIME] => 3826718 [DURATION] => 4200 [TITLE] => Chapter 23 [DESCRIPTION] => Chapter 23\’s Description ) [2] => stdClass Object ( [ID] => 47 [COURSE_ID] => 4 [START_TIME] => 3653545 [END_TIME] => 3658238 [DURATION] => 4200 [TITLE] => Chapter 22 [DESCRIPTION] => Chapter 22\’s Description ) [3] => stdClass Object ( [ID] => 46 [COURSE_ID] => 4 [START_TIME] => 3490243 [END_TIME] => 3495917 [DURATION] => 4200 [TITLE] => Chapter 21 [DESCRIPTION] => Chapter 21\’s Description ) [4] => stdClass Object ( [ID] => 45 [COURSE_ID] => 4 [START_TIME] => 3338210 [END_TIME] => 3343938 [DURATION] => 4200 [TITLE] => Chapter 20 [DESCRIPTION] => Chapter 20\’s Description ) [5] => stdClass Object ( [ID] => 44 [COURSE_ID] => 4 [START_TIME] => 3169837 [END_TIME] => 3174154 [DURATION] => 4200 [TITLE] => Chapter 19 [DESCRIPTION] => Chapter 19\’s Description ) [6] => stdClass Object ( [ID] => 43 [COURSE_ID] => 4 [START_TIME] => 3016815 [END_TIME] => 3020020 [DURATION] => 4200 [TITLE] => Chapter 18 [DESCRIPTION] => Chapter 18\’s Description ) [7] => stdClass Object ( [ID] => 42 [COURSE_ID] => 4 [START_TIME] => 2858254 [END_TIME] => 2862309 [DURATION] => 4200 [TITLE] => Chapter 17 [DESCRIPTION] => Chapter 17\’s Description ) [8] => stdClass Object ( [ID] => 41 [COURSE_ID] => 4 [START_TIME] => 2703813 [END_TIME] => 2706641 [DURATION] => 4200 [TITLE] => Chapter 16 [DESCRIPTION] => Chapter 16\’s Description ) [9] => stdClass Object ( [ID] => 40 [COURSE_ID] => 4 [START_TIME] => 2551822 [END_TIME] => 2555208 [DURATION] => 4200 [TITLE] => Chapter 15 [DESCRIPTION] => Chapter 15\’s Description ) [10] => stdClass Object ( [ID] => 39 [COURSE_ID] => 4 [START_TIME] => 2382407 [END_TIME] => 2387901 [DURATION] => 4200 [TITLE] => Chapter 14 [DESCRIPTION] => Chapter 14\’s Description ) [11] => stdClass Object ( [ID] => 38 [COURSE_ID] => 4 [START_TIME] => 2225619 [END_TIME] => 2231232 [DURATION] => 4200 [TITLE] => Chapter 13 [DESCRIPTION] => Chapter 13\’s Description ) [12] => stdClass Object ( [ID] => 37 [COURSE_ID] => 4 [START_TIME] => 2064552 [END_TIME] => 2068656 [DURATION] => 4200 [TITLE] => Chapter 12 [DESCRIPTION] => Chapter 12\’s Description ) [13] => stdClass Object ( [ID] => 36 [COURSE_ID] => 4 [START_TIME] => 1913475 [END_TIME] => 1916306 [DURATION] => 4200 [TITLE] => Chapter 11 [DESCRIPTION] => Chapter 11\’s Description ) [14] => stdClass Object ( [ID] => 35 [COURSE_ID] => 4 [START_TIME] => 1750950 [END_TIME] => 1756908 [DURATION] => 4200 [TITLE] => Chapter 10 [DESCRIPTION] => Chapter 10\’s Description ) [15] => stdClass Object ( [ID] => 34 [COURSE_ID] => 4 [START_TIME] => 1587965 [END_TIME] => 1592709 [DURATION] => 4200 [TITLE] => Chapter 9 [DESCRIPTION] => Chapter 9\’s Description ) [16] => stdClass Object ( [ID] => 33 [COURSE_ID] => 4 [START_TIME] => 1431054 [END_TIME] => 1435008 [DURATION] => 4200 [TITLE] => Chapter 8 [DESCRIPTION] => Chapter 8\’s Description ) [17] => stdClass Object ( [ID] => 32 [COURSE_ID] => 4 [START_TIME] => 1280421 [END_TIME] => 1283872 [DURATION] => 4200 [TITLE] => Chapter 7 [DESCRIPTION] => Chapter 7\’s Description ) [18] => stdClass Object ( [ID] => 31 [COURSE_ID] => 4 [START_TIME] => 1123068 [END_TIME] => 1128464 [DURATION] => 4200 [TITLE] => Chapter 6 [DESCRIPTION] => Chapter 6\’s Description ) [19] => stdClass Object ( [ID] => 30 [COURSE_ID] => 4 [START_TIME] => 960918 [END_TIME] => 966370 [DURATION] => 4200 [TITLE] => Chapter 5 [DESCRIPTION] => Chapter 5\’s Description ) [20] => stdClass Object ( [ID] => 29 [COURSE_ID] => 4 [START_TIME] => 793943 [END_TIME] => 797123 [DURATION] => 4200 [TITLE] => Chapter 4 [DESCRIPTION] => Chapter 4\’s Description ) [21] => stdClass Object ( [ID] => 28 [COURSE_ID] => 4 [START_TIME] => 638285 [END_TIME] => 641130 [DURATION] => 4200 [TITLE] => Chapter 3 [DESCRIPTION] => Chapter 3\’s Description ) [22] => stdClass Object ( [ID] => 27 [COURSE_ID] => 4 [START_TIME] => 479384 [END_TIME] => 483993 [DURATION] => 4200 [TITLE] => Chapter 2 [DESCRIPTION] => Chapter 2\’s Description ) [23] => stdClass Object ( [ID] => 26 [COURSE_ID] => 4 [START_TIME] => 320101 [END_TIME] => 325435 [DURATION] => 4200 [TITLE] => Chapter 1 [DESCRIPTION] => Chapter 1\’s Description ) [24] => stdClass Object ( [ID] => 25 [COURSE_ID] => 4 [START_TIME] => 163844 [END_TIME] => 166828 [DURATION] => 2100 [TITLE] => Introductinon [DESCRIPTION] => A brief description of the course chapter’s taken one by one along with the main notions and theories. ) ) ) )