How to use
Install via yarn add locutus and import:
import { array_change_key_case } from 'locutus/php/array/array_change_key_case'.
Or with CommonJS: const { array_change_key_case } = require('locutus/php/array/array_change_key_case')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite ,
webpack ,
Rollup , and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
# code expected result
1 array_change_key_case(42)false
2 array_change_key_case([ 3, 5 ])[3, 5]
3 array_change_key_case({ FuBaR: 42 }){"fubar": 42}
4 array_change_key_case({ FuBaR: 42 }, 'CASE_LOWER'){"fubar": 42}
5 array_change_key_case({ FuBaR: 42 }, 'CASE_UPPER'){"FUBAR": 42}
6 array_change_key_case({ FuBaR: 42 }, 2){"FUBAR": 42}
PHP arrays and TypeScript/JavaScript
Please note that Locutus uses TypeScript/JavaScript objects as substitutes for PHP arrays,
they are the closest we can get to this hashtable-like data structure without
rolling our own. While many TypeScript/JavaScript implementations preserve the order of object properties, the
ECMAScript Language Specification
explicitly states that:
The mechanics and order of enumerating the properties is not specified.
In practice most engines preserve insertion order, but if your code depends on key ordering across platforms, keep this caveat in mind.
To influence how Locutus treats objects as arrays, you can check out the locutus.objectsAsArrays setting.
Dependencies
This function uses the following Locutus functions:
Here's what our current TypeScript equivalent to PHP's array_change_key_case looks like.
Copy code
import { type PhpArrayLike , type PhpAssoc , type PhpRuntimeValue , toPhpArrayObject } from '../_helpers/_phpTypes.ts' type ChangeValue = PhpRuntimeValue type ArrayChangeInput <TValue extends ChangeValue > = number | PhpArrayLike <TValue > | null type ChangeKeyCaseMode = 0 | 1 | 2 | 'CASE_LOWER' | 'CASE_UPPER' export function array_change_key_case (array : number | null , cs ?: ChangeKeyCaseMode ): false export function array_change_key_case<TValue extends ChangeValue >(array : TValue [], cs ?: ChangeKeyCaseMode ): TValue []export function array_change_key_case<TValue extends ChangeValue >( array : PhpAssoc <TValue >, cs ?: ChangeKeyCaseMode , ): PhpAssoc <TValue > export function array_change_key_case<TValue extends ChangeValue >( array : ArrayChangeInput <TValue >, cs ?: ChangeKeyCaseMode , ): PhpArrayLike <TValue > | false { let result : PhpArrayLike <TValue > | false if (Array .isArray (array)) { result = array } else if (!array || typeof array !== 'object' ) { result = false } else { const caseFunction : 'toLowerCase' | 'toUpperCase' = cs === undefined || cs === 0 || cs === 'CASE_LOWER' ? 'toLowerCase' : 'toUpperCase' const source = toPhpArrayObject<TValue >(array) const transformed : PhpAssoc <TValue > = {} for (const [key, value] of Object .entries (source)) { transformed[key[caseFunction]()] = value } result = transformed } return result }
import { toPhpArrayObject } from '../_helpers/_phpTypes.ts' export function array_change_key_case (array, cs ) { let result if (Array .isArray (array)) { result = array } else if (!array || typeof array !== 'object' ) { result = false } else { const caseFunction = cs === undefined || cs === 0 || cs === 'CASE_LOWER' ? 'toLowerCase' : 'toUpperCase' const source = toPhpArrayObject (array) const transformed = {} for (const [key, value] of Object .entries (source)) { transformed[key[caseFunction]()] = value } result = transformed } return result }
type PhpNullish = null | undefined type PhpInput = {} | PhpNullish type PhpScalar = string | number | boolean type PhpPrimitive = PhpScalar | bigint type PhpList <T = PhpInput > = T[]type PhpAssoc <T = PhpInput > = { [key : string ]: T }type PhpArrayLike <T = PhpInput > = PhpList <T> | PhpAssoc <T>type PhpFunctionValue = (...args : PhpInput [] ) => PhpInput interface PhpRuntimeAssoc { [key : string ]: PhpRuntimeValue } interface PhpRuntimeList extends Array <PhpRuntimeValue > {}type PhpRuntimeValue = PhpPrimitive | PhpNullish | PhpRuntimeList | PhpRuntimeAssoc | PhpFunctionValue function isObjectLike (value : PhpInput ): value is PhpArrayLike <PhpInput > { return typeof value === 'object' && value !== null } function isPhpArrayObject<T = PhpInput >(value : PhpInput ): value is PhpAssoc <T> { return isObjectLike (value) } function toPhpArrayObject<T = PhpInput >(value : PhpInput ): PhpAssoc <T> { if (isPhpArrayObject<T>(value)) { return value } return {} } type ChangeValue = PhpRuntimeValue type ArrayChangeInput <TValue extends ChangeValue > = number | PhpArrayLike <TValue > | null type ChangeKeyCaseMode = 0 | 1 | 2 | 'CASE_LOWER' | 'CASE_UPPER' function array_change_key_case (array : number | null , cs ?: ChangeKeyCaseMode ): false function array_change_key_case<TValue extends ChangeValue >(array : TValue [], cs ?: ChangeKeyCaseMode ): TValue []function array_change_key_case<TValue extends ChangeValue >( array : PhpAssoc <TValue >, cs ?: ChangeKeyCaseMode , ): PhpAssoc <TValue > function array_change_key_case<TValue extends ChangeValue >( array : ArrayChangeInput <TValue >, cs ?: ChangeKeyCaseMode , ): PhpArrayLike <TValue > | false { let result : PhpArrayLike <TValue > | false if (Array .isArray (array)) { result = array } else if (!array || typeof array !== 'object' ) { result = false } else { const caseFunction : 'toLowerCase' | 'toUpperCase' = cs === undefined || cs === 0 || cs === 'CASE_LOWER' ? 'toLowerCase' : 'toUpperCase' const source = toPhpArrayObject<TValue >(array) const transformed : PhpAssoc <TValue > = {} for (const [key, value] of Object .entries (source)) { transformed[key[caseFunction]()] = value } result = transformed } return result }
function isPhpArrayObject (value ) { return typeof value === 'object' && value !== null } function toPhpArrayObject (value ) { if (isPhpArrayObject (value)) { return value } return {} } function array_change_key_case (array, cs ) { let result if (Array .isArray (array)) { result = array } else if (!array || typeof array !== 'object' ) { result = false } else { const caseFunction = cs === undefined || cs === 0 || cs === 'CASE_LOWER' ? 'toLowerCase' : 'toUpperCase' const source = toPhpArrayObject (array) const transformed = {} for (const [key, value] of Object .entries (source)) { transformed[key[caseFunction]()] = value } result = transformed } return result }
Improve this function
Locutus is a community effort following
The McDonald's Theory :
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
View on GitHub
·
Edit on GitHub
·
View Raw
« More PHP array functions
Star