PHP's array_column in TypeScript

How to use

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

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

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_column([{name: 'Alex', value: 1}, {name: 'Elvis', value: 2}, {name: 'Michael', value: 3}], 'name'){0: "Alex", 1: "Elvis", 2: "Michael"}
2array_column({0: {name: 'Alex', value: 1}, 1: {name: 'Elvis', value: 2}, 2: {name: 'Michael', value: 3}}, 'name'){0: "Alex", 1: "Elvis", 2: "Michael"}
3array_column([{name: 'Alex', value: 1}, {name: 'Elvis', value: 2}, {name: 'Michael', value: 3}], 'name', 'value'){1: "Alex", 2: "Elvis", 3: "Michael"}
4array_column([{name: 'Alex', value: 1}, {name: 'Elvis', value: 2}, {name: 'Michael', value: 3}], null, 'value'){1: {name: 'Alex', value: 1}, 2: {name: 'Elvis', value: 2}, 3: {name: 'Michael', value: 3}}

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_column looks like.

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

type ColumnRow<TValue> = PhpAssoc<TValue>
type ColumnInput<TValue> = ColumnRow<TValue>[] | PhpAssoc<ColumnRow<TValue>>
type ColumnOutput<TValue> = PhpAssoc<TValue | ColumnRow<TValue> | undefined>

export function array_column<TValue>(
input: ColumnInput<TValue>,
columnKey: string | number | null,
indexKey?: string | number | null,
): ColumnOutput<TValue>
export function array_column<TValue>(
input: ColumnInput<TValue> | null | undefined,
columnKey: string | number | null,
indexKey?: string | number | null,
): ColumnOutput<TValue> | undefined {
// discuss at: https://locutus.io/php/array_column/
// original by: Enzo Dañobeytía
// example 1: array_column([{name: 'Alex', value: 1}, {name: 'Elvis', value: 2}, {name: 'Michael', value: 3}], 'name')
// returns 1: {0: "Alex", 1: "Elvis", 2: "Michael"}
// example 2: array_column({0: {name: 'Alex', value: 1}, 1: {name: 'Elvis', value: 2}, 2: {name: 'Michael', value: 3}}, 'name')
// returns 2: {0: "Alex", 1: "Elvis", 2: "Michael"}
// example 3: array_column([{name: 'Alex', value: 1}, {name: 'Elvis', value: 2}, {name: 'Michael', value: 3}], 'name', 'value')
// returns 3: {1: "Alex", 2: "Elvis", 3: "Michael"}
// example 4: array_column([{name: 'Alex', value: 1}, {name: 'Elvis', value: 2}, {name: 'Michael', value: 3}], null, 'value')
// returns 4: {1: {name: 'Alex', value: 1}, 2: {name: 'Elvis', value: 2}, 3: {name: 'Michael', value: 3}}

if (input === null || typeof input !== 'object') {
return undefined
}

const normalizedInput = Array.isArray(input) ? input : Object.values(toPhpArrayObject<ColumnRow<TValue>>(input))
const result: ColumnOutput<TValue> = {}
let fallbackIndex = 0

for (const rowValue of normalizedInput) {
const row = toPhpArrayObject<TValue>(rowValue)
const indexCandidate = indexKey === null ? undefined : row[String(indexKey)]

const value = columnKey === null ? rowValue : row[String(columnKey)]
if (indexCandidate !== undefined && indexCandidate !== null) {
result[String(indexCandidate)] = value
} else {
result[String(fallbackIndex)] = value
fallbackIndex += 1
}
}

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