PHP's array_count_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_count_values } from 'locutus/php/array/array_count_values'.

Or with CommonJS: const { array_count_values } = require('locutus/php/array/array_count_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_count_values([ 3, 5, 3, "foo", "bar", "foo" ]){3:2, 5:1, "foo":2, "bar":1}
2array_count_values({ p1: 3, p2: 5, p3: 3, p4: "foo", p5: "bar", p6: "foo" }){3:2, 5:1, "foo":2, "bar":1}
3array_count_values([ true, 4.2, 42, "fubar" ]){42:1, "fubar":1}

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

import {
isObjectLike,
type PhpArrayLike,
type PhpAssoc,
type PhpRuntimeValue,
toPhpArrayObject,
} from '../_helpers/_phpTypes.ts'

type CountValue = PhpRuntimeValue

export function array_count_values(array: PhpArrayLike<CountValue>): PhpAssoc<number> {
// discuss at: https://locutus.io/php/array_count_values/
// parity verified: PHP 8.3
// original by: Ates Goral (https://magnetiq.com)
// improved by: Michael White (https://getsprink.com)
// improved by: Kevin van Zonneveld (https://kvz.io)
// input by: sankai
// input by: Shingo
// bugfixed by: Brett Zamir (https://brett-zamir.me)
// example 1: array_count_values([ 3, 5, 3, "foo", "bar", "foo" ])
// returns 1: {3:2, 5:1, "foo":2, "bar":1}
// example 2: array_count_values({ p1: 3, p2: 5, p3: 3, p4: "foo", p5: "bar", p6: "foo" })
// returns 2: {3:2, 5:1, "foo":2, "bar":1}
// example 3: array_count_values([ true, 4.2, 42, "fubar" ])
// returns 3: {42:1, "fubar":1}

const tmpArr: PhpAssoc<number> = {}

const _countValue = function (target: PhpAssoc<number>, value: CountValue): void {
let normalized = ''
if (typeof value === 'number') {
if (Math.floor(value) !== value) {
return
}
normalized = String(value)
} else if (typeof value !== 'string') {
return
} else {
normalized = value
}

if (normalized in target && Object.hasOwn(target, normalized)) {
target[normalized] = (target[normalized] ?? 0) + 1
} else {
target[normalized] = 1
}
}

if (!isObjectLike(array)) {
return tmpArr
}

const source = toPhpArrayObject<CountValue>(array)
for (const value of Object.values(source)) {
_countValue(tmpArr, value)
}

return tmpArr
}

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