Install via yarn add locutus and import:
import { array_map } from 'locutus/php/array/array_map'.
Or with CommonJS: const { array_map } = require('locutus/php/array/array_map')
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_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] )
[ 1, 8, 27, 64, 125 ]
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.
Notes
If the callback is a string (or object, if an array is supplied),
it can only work if the function name is in the global context
Dependencies
This function uses the following Locutus functions:
exportfunction array_map<TResult = ArrayMapValue>( callback: PhpCallableDescriptor<ArrayMapCallbackArgsVariadic, TResult> | null | undefined, ...inputArrays: ArrayMapInputs ): PhpList<TResult> | PhpList<ArrayMapCallbackArgsVariadic> { // discuss at: https://locutus.io/php/array_map/ // original by: Andrea Giammarchi (https://webreflection.blogspot.com) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Brett Zamir (https://brett-zamir.me) // input by: thekid // note 1: If the callback is a string (or object, if an array is supplied), // note 1: it can only work if the function name is in the global context // example 1: array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] ) // returns 1: [ 1, 8, 27, 64, 125 ]
exportfunctionarray_map(callback, ...inputArrays) { // discuss at: https://locutus.io/php/array_map/ // original by: Andrea Giammarchi (https://webreflection.blogspot.com) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Brett Zamir (https://brett-zamir.me) // input by: thekid // note 1: If the callback is a string (or object, if an array is supplied), // note 1: it can only work if the function name is in the global context // example 1: array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] ) // returns 1: [ 1, 8, 27, 64, 125 ]
functionisObjectLike(value: PhpInput): value is PhpArrayLike<PhpInput> { returntypeof value === 'object' && value !== null }
function isPhpCallable<TArgsextendsPhpCallableArgs = PhpCallableArgs, TResult = PhpInput>( value: PhpInput, ): value is PhpCallable<TArgs, TResult> { returntypeof value === 'function' }
function array_map<TInputsextendsArrayMapInputs, TResult>( callback: PhpCallableDescriptor<ArrayMapTupleArgs<TInputs>, TResult>, ...inputArrays: TInputs ): PhpList<TResult>
function array_map<TInputsextendsArrayMapInputs>( callback: null | undefined, ...inputArrays: TInputs ): PhpList<ArrayMapTupleArgs<TInputs>>
function array_map<TResult = ArrayMapValue>( callback: PhpCallableDescriptor<ArrayMapCallbackArgsVariadic, TResult> | null | undefined, ...inputArrays: ArrayMapInputs ): PhpList<TResult> | PhpList<ArrayMapCallbackArgsVariadic> { // discuss at: https://locutus.io/php/array_map/ // original by: Andrea Giammarchi (https://webreflection.blogspot.com) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Brett Zamir (https://brett-zamir.me) // input by: thekid // note 1: If the callback is a string (or object, if an array is supplied), // note 1: it can only work if the function name is in the global context // example 1: array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] ) // returns 1: [ 1, 8, 27, 64, 125 ]
functionarray_map(callback, ...inputArrays) { // discuss at: https://locutus.io/php/array_map/ // original by: Andrea Giammarchi (https://webreflection.blogspot.com) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Brett Zamir (https://brett-zamir.me) // input by: thekid // note 1: If the callback is a string (or object, if an array is supplied), // note 1: it can only work if the function name is in the global context // example 1: array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] ) // returns 1: [ 1, 8, 27, 64, 125 ]
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.