PHP's array_values in TypeScript

✓ Verified: PHP 8.3
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.

How to use

Install via yarn add locutus and import: import { array_values } from 'locutus/php/array/array_values'.

Or with CommonJS: const { array_values } = require('locutus/php/array/array_values')

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.

#codeexpected result
1array_values( {firstname: 'Kevin', surname: 'van Zonneveld'} )[ 'Kevin', 'van Zonneveld' ]

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.

Here's what our current TypeScript equivalent to PHP's array_values looks like.

import type { PhpAssoc } from '../_helpers/_phpTypes.ts'

type ArrayValuesInput = object | string | number | boolean | bigint | null | undefined

const isBoxedScalar = (value: ArrayValuesInput): value is String | Number | Boolean => {
if (typeof value !== 'object' || value === null) {
return false
}

const objectTag = Object.prototype.toString.call(value)
return objectTag === '[object String]' || objectTag === '[object Number]' || objectTag === '[object Boolean]'
}

const describeArrayValuesArgument = (value: ArrayValuesInput): string => {
if (typeof value === 'string') {
return 'string'
}

if (typeof value === 'boolean') {
return 'bool'
}

if (typeof value === 'number') {
return Number.isInteger(value) ? 'int' : 'float'
}

if (typeof value === 'bigint') {
return 'int'
}

if (typeof value === 'undefined' || value === null) {
return 'null'
}

if (typeof value === 'object') {
const objectTag = Object.prototype.toString.call(value)

if (objectTag === '[object String]') {
return 'string'
}

if (objectTag === '[object Number]') {
return Number.isInteger(Number(value.valueOf())) ? 'int' : 'float'
}

if (objectTag === '[object Boolean]') {
return 'bool'
}

return 'object'
}

return typeof value
}

export function array_values<T>(input: T[] | PhpAssoc<T>): T[] {
// discuss at: https://locutus.io/php/array_values/
// parity verified: PHP 8.3
// original by: Kevin van Zonneveld (https://kvz.io)
// improved by: Brett Zamir (https://brett-zamir.me)
// example 1: array_values( {firstname: 'Kevin', surname: 'van Zonneveld'} )
// returns 1: [ 'Kevin', 'van Zonneveld' ]

if (typeof input !== 'object' || input === null || isBoxedScalar(input)) {
throw new TypeError(
`array_values(): Argument #1 ($array) must be of type array, ${describeArrayValuesArgument(input)} given`,
)
}

return Object.values(input)
}

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