How to use
Install via yarn add locutus and import:
import { array_walk_recursive } from 'locutus/php/array/array_walk_recursive'.
Or with CommonJS: const { array_walk_recursive } = require('locutus/php/array/array_walk_recursive')
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_walk_recursive([3, 4], function () {}, 'userdata')true
2 array_walk_recursive([3, [4]], function () {}, 'userdata')true
3 array_walk_recursive([3, []], function () {}, 'userdata')true
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
Dependencies
This function uses the following Locutus functions:
Here's what our current TypeScript equivalent to PHP's array_walk_recursive looks like.
Copy code
import { isObjectLike, type PhpAssoc , type PhpRuntimeList , type PhpRuntimeValue , toPhpArrayObject, } from '../_helpers/_phpTypes.ts' type RecursiveWalkNode <TValue > = TValue | RecursiveWalkList <TValue >type RecursiveWalkList <TValue > = RecursiveWalkNode <TValue >[]type RecursiveWalkAssoc <TValue > = PhpAssoc <RecursiveWalkNode <TValue >>type RecursiveWalkCollection <TValue > = RecursiveWalkList <TValue > | RecursiveWalkAssoc <TValue >type RecursiveWalkInput = Exclude <PhpRuntimeValue , PhpRuntimeList >type ArrayWalkRecursiveCallback <TKey extends number | string , TValue , TUserdata > = ( value : TValue , key : TKey , userdata ?: TUserdata , ) => void export function array_walk_recursive< TValue extends RecursiveWalkInput = RecursiveWalkInput , TUserdata extends RecursiveWalkInput = RecursiveWalkInput , >( array : RecursiveWalkList <TValue >, funcname : ArrayWalkRecursiveCallback <number , TValue , TUserdata >, userdata ?: TUserdata , ): boolean export function array_walk_recursive< TValue extends RecursiveWalkInput = RecursiveWalkInput , TUserdata extends RecursiveWalkInput = RecursiveWalkInput , >( array : RecursiveWalkAssoc <TValue >, funcname : ArrayWalkRecursiveCallback <string , TValue , TUserdata >, userdata ?: TUserdata , ): boolean export function array_walk_recursive< TValue extends RecursiveWalkInput = RecursiveWalkInput , TUserdata extends RecursiveWalkInput = RecursiveWalkInput , >( array : RecursiveWalkCollection <TValue >, funcname : | ArrayWalkRecursiveCallback <number , TValue , TUserdata > | ArrayWalkRecursiveCallback <string , TValue , TUserdata >, userdata ?: TUserdata , ): boolean { if (!isObjectLike (array)) { return false } if (typeof funcname !== 'function' ) { return false } const hasUserdata = typeof userdata !== 'undefined' const callCallback = (value : TValue , key : number | string ): boolean => { try { if (hasUserdata) { Reflect .apply (funcname, undefined , [value, key, userdata]) } else { Reflect .apply (funcname, undefined , [value, key]) } return true } catch (_e) { return false } } const walkList = (list : RecursiveWalkList <TValue >): boolean => { for (const [index, value] of list.entries ()) { if (Array .isArray (value)) { if (!walkList (value)) { return false } continue } if (!callCallback (value, index)) { return false } } return true } const walkAssoc = (assoc : RecursiveWalkAssoc <TValue >): boolean => { for (const [key, value] of Object .entries (assoc)) { if (Array .isArray (value)) { if (!walkList (value)) { return false } continue } if (!callCallback (value, key)) { return false } } return true } if (Array .isArray (array)) { return walkList (array) } return walkAssoc (toPhpArrayObject<RecursiveWalkNode <TValue >>(array)) }
import { isObjectLike, toPhpArrayObject } from '../_helpers/_phpTypes.ts' export function array_walk_recursive (array, funcname, userdata ) { if (!isObjectLike (array)) { return false } if (typeof funcname !== 'function' ) { return false } const hasUserdata = typeof userdata !== 'undefined' const callCallback = (value, key ) => { try { if (hasUserdata) { Reflect .apply (funcname, undefined , [value, key, userdata]) } else { Reflect .apply (funcname, undefined , [value, key]) } return true } catch (_e) { return false } } const walkList = (list ) => { for (const [index, value] of list.entries ()) { if (Array .isArray (value)) { if (!walkList (value)) { return false } continue } if (!callCallback (value, index)) { return false } } return true } const walkAssoc = (assoc ) => { for (const [key, value] of Object .entries (assoc)) { if (Array .isArray (value)) { if (!walkList (value)) { return false } continue } if (!callCallback (value, key)) { return false } } return true } if (Array .isArray (array)) { return walkList (array) } return walkAssoc (toPhpArrayObject (array)) }
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 RecursiveWalkNode <TValue > = TValue | RecursiveWalkList <TValue >type RecursiveWalkList <TValue > = RecursiveWalkNode <TValue >[]type RecursiveWalkAssoc <TValue > = PhpAssoc <RecursiveWalkNode <TValue >>type RecursiveWalkCollection <TValue > = RecursiveWalkList <TValue > | RecursiveWalkAssoc <TValue >type RecursiveWalkInput = Exclude <PhpRuntimeValue , PhpRuntimeList >type ArrayWalkRecursiveCallback <TKey extends number | string , TValue , TUserdata > = ( value : TValue , key : TKey , userdata ?: TUserdata , ) => void function array_walk_recursive< TValue extends RecursiveWalkInput = RecursiveWalkInput , TUserdata extends RecursiveWalkInput = RecursiveWalkInput , >( array : RecursiveWalkList <TValue >, funcname : ArrayWalkRecursiveCallback <number , TValue , TUserdata >, userdata ?: TUserdata , ): boolean function array_walk_recursive< TValue extends RecursiveWalkInput = RecursiveWalkInput , TUserdata extends RecursiveWalkInput = RecursiveWalkInput , >( array : RecursiveWalkAssoc <TValue >, funcname : ArrayWalkRecursiveCallback <string , TValue , TUserdata >, userdata ?: TUserdata , ): boolean function array_walk_recursive< TValue extends RecursiveWalkInput = RecursiveWalkInput , TUserdata extends RecursiveWalkInput = RecursiveWalkInput , >( array : RecursiveWalkCollection <TValue >, funcname : | ArrayWalkRecursiveCallback <number , TValue , TUserdata > | ArrayWalkRecursiveCallback <string , TValue , TUserdata >, userdata ?: TUserdata , ): boolean { if (!isObjectLike (array)) { return false } if (typeof funcname !== 'function' ) { return false } const hasUserdata = typeof userdata !== 'undefined' const callCallback = (value : TValue , key : number | string ): boolean => { try { if (hasUserdata) { Reflect .apply (funcname, undefined , [value, key, userdata]) } else { Reflect .apply (funcname, undefined , [value, key]) } return true } catch (_e) { return false } } const walkList = (list : RecursiveWalkList <TValue >): boolean => { for (const [index, value] of list.entries ()) { if (Array .isArray (value)) { if (!walkList (value)) { return false } continue } if (!callCallback (value, index)) { return false } } return true } const walkAssoc = (assoc : RecursiveWalkAssoc <TValue >): boolean => { for (const [key, value] of Object .entries (assoc)) { if (Array .isArray (value)) { if (!walkList (value)) { return false } continue } if (!callCallback (value, key)) { return false } } return true } if (Array .isArray (array)) { return walkList (array) } return walkAssoc (toPhpArrayObject<RecursiveWalkNode <TValue >>(array)) }
function isObjectLike (value ) { return typeof value === 'object' && value !== null } function toPhpArrayObject (value ) { if (isPhpArrayObject (value)) { return value } return {} } const isPhpArrayObject = isObjectLikefunction array_walk_recursive (array, funcname, userdata ) { if (!isObjectLike (array)) { return false } if (typeof funcname !== 'function' ) { return false } const hasUserdata = typeof userdata !== 'undefined' const callCallback = (value, key ) => { try { if (hasUserdata) { Reflect .apply (funcname, undefined , [value, key, userdata]) } else { Reflect .apply (funcname, undefined , [value, key]) } return true } catch (_e) { return false } } const walkList = (list ) => { for (const [index, value] of list.entries ()) { if (Array .isArray (value)) { if (!walkList (value)) { return false } continue } if (!callCallback (value, index)) { return false } } return true } const walkAssoc = (assoc ) => { for (const [key, value] of Object .entries (assoc)) { if (Array .isArray (value)) { if (!walkList (value)) { return false } continue } if (!callCallback (value, key)) { return false } } return true } if (Array .isArray (array)) { return walkList (array) } return walkAssoc (toPhpArrayObject (array)) }
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