Oihana PHP

hasStringKeys.php

Table of Contents

Functions

hasStringKeys()  : bool
Indicates if all the keys in an array are strings.

Functions

hasStringKeys()

Indicates if all the keys in an array are strings.

hasStringKeys(array<string|int, mixed> $array) : bool

This function checks if every key in the provided array is a string. Returns true only if all keys are strings, false otherwise.

Parameters
$array : array<string|int, mixed>

The array to check.

Tags
example
$arr1 = ['foo' => 1, 'bar' => 2, 'baz' => 3];
var_dump(hasStringKeys($arr1)); // true

$arr2 = ['0' => 'a', '1' => 'b', '2' => 'c'];
var_dump(hasStringKeys($arr2)); // true, keys are strings '0', '1', '2'

$arr3 = [0 => 'zero', 1 => 'one', 2 => 'two'];
var_dump(hasStringKeys($arr3)); // false, keys are integers

$arr4 = ['foo' => 'bar', 42 => 'baz'];
var_dump(hasStringKeys($arr4)); // false, mixed keys (string and int)

$arr5 = [];
var_dump(hasStringKeys($arr5)); // true (empty array, vacuously true)
Return values
bool

True if all keys are strings, false otherwise.


        
On this page

Search results