array_is_list
array_is_list(mixed $value): bool
Whether the array is a "true list" (sequential 0..n-1 integer keys); the health-check tool for PHP's two array faces - json_encode emits an array or an object depending on which face shows.
C C23
#include <stdio.h>
/* A C array IS a list by construction: contiguous, index 0..n-1.
"Dictionary" needs hand-written structures - the distinction
lives in the type system you build yourself */
int main(void) {
int list[3] = { 1, 2, 3 };
printf("%d %d %d\n", list[0], list[1], list[2]);
return 0;
}
1 2 3
C++ C++26
#include <iostream>
#include <map>
#include <string>
#include <vector>
int main() {
// the list/dictionary distinction is static: vector vs map
std::vector<std::string> list = { "a", "b", "c" };
std::map<std::string, int> dict = { { "x", 1 }, { "y", 2 } };
std::cout << list[0] << "\n"; // a
std::cout << dict["x"] << "\n"; // 1
std::cout << list.size() << "\n"; // 3
return 0;
}
a 1 3
C# v11.0.100
using System.Collections.Generic;
class MainClass {
static void Main() {
// List<T> and Dictionary<K,V> are different types - no checking needed
var list = new List<string> { "a", "b", "c" };
var dict = new Dictionary<string, int> { ["x"] = 1, ["y"] = 2 };
System.Console.WriteLine(list[0]); // a
System.Console.WriteLine(dict["x"]); // 1
System.Console.WriteLine(string.Join(",", list)); // a,b,c
}
}
a 1 a,b,c
Go v1.27.4
package main
import "fmt"
func main() {
// slices and maps are different types - the "two faces" of PHP
// arrays are two separate types in Go
list := []string{"a", "b", "c"}
dict := map[string]int{"x": 1, "y": 2}
fmt.Println(list[0]) // a
fmt.Println(dict["x"]) // 1
fmt.Printf("%T\n", list) // []string
fmt.Printf("%T\n", dict) // map[string]int
}
a 1 []string map[string]int
Java v27.0.1
import java.util.List;
import java.util.Map;
class Main {
public static void main(String[] args) {
List<String> list = List.of("a", "b", "c");
Map<String, Integer> dict = Map.of("x", 1, "y", 2);
System.out.println(list.get(0)); // a
System.out.println(dict.get("x")); // 1
System.out.println(list instanceof java.util.List); // true
}
}
a 1 true
JavaScript v26.13.1
const list = ["a", "b", "c"];
const dict = { x: 1, y: 2 };
console.log(Array.isArray(list)); // true
console.log(Array.isArray(dict)); // false
console.log(JSON.stringify(list)); // ["a","b","c"] - array face
console.log(JSON.stringify(dict)); // {"x":1,"y":2} - object face
true
false
["a","b","c"]
{"x":1,"y":2}
PHP v8.6.0
$list = ["a", "b", "c"]; // 0..n-1 sequential keys
$dict = ["x" => 1, "y" => 2]; // string keys: dictionary face
$odd = [1 => "one", 0 => "zero"]; // same pairs, out of order
echo var_export(array_is_list($list), true), "\n"; // true
echo var_export(array_is_list($dict), true), "\n"; // false
echo var_export(array_is_list($odd), true), "\n"; // false: key order breaks it
echo json_encode($list), "\n"; // ["a","b","c"] - array face
echo json_encode($dict), "\n"; // {"x":1,"y":2} - object face
echo json_encode($odd), "\n"; // {"1":"one","0":"zero"} - insertion order kept
true
false
false
["a","b","c"]
{"x":1,"y":2}
{"1":"one","0":"zero"}
Python v3.15.2
import json
lst = ["a", "b", "c"]
dct = {"x": 1, "y": 2}
print(type(lst).__name__) # list - a first-class type
print(type(dct).__name__) # dict
print(json.dumps(lst)) # ["a", "b", "c"] - array face
print(json.dumps(dct)) # {"x": 1, "y": 2} - object face
list
dict
["a", "b", "c"]
{"x": 1, "y": 2}
Rust v1.99.1
// Vec vs HashMap: two separate types, checked by the compiler
use std::collections::HashMap;
fn main() {
let list = vec!["a", "b", "c"];
let mut dict = HashMap::new();
dict.insert("x", 1);
dict.insert("y", 2);
println!("{:?}", list); // ["a", "b", "c"]
println!("{}", list.len()); // 3
println!("{}", dict["x"]); // 1
}
["a", "b", "c"] 3 1
TypeScript v26.13.1
const list: string[] = ["a", "b", "c"];
const dict: Record<string, number> = { x: 1, y: 2 };
console.log(Array.isArray(list)); // true
console.log(Array.isArray(dict)); // false
// compile time keeps them apart too:
const first: string = list[0];
console.log(first); // a
true false a
Language differences
C: arrays are lists by construction (example) - contiguous storage + integer indexing is a language contract; a "dictionary" is a hand-built hash table, the two faces are two data structures in C.
C++: std::vector vs std::map, statically distinguished (example); note std::map iterates in key order - std::unordered_map matches PHP's hash semantics.
C#: List<T>/Dictionary<K,V>, the type is the semantics (example); C# arrays T[] are natural lists too; JSON serialization behaves like PHP/Python/JS (lists emit arrays, dictionaries emit objects).
Go: slice and map are two types (shown with %T in the example); Go maps iterate in random order - even more "dictionary" than PHP; json.Marshal likewise picks array/object by type.
Java: the List/Map interface families (example); instanceof for typing; Jackson/Gson also decide JSON shape by type; converting arrays to lists via Arrays.asList is fixed-length - "listness" is a compile-time fact in Java.
JavaScript: Array.isArray is the counterpart (example); JSON.stringify emits array/object by type - isomorphic to json_encode, but JS never surprises you with "an array that serializes as an object" because types never mix.
PHP: the core teaching (three states + JSON contrast in the example) - PHP has ONE array type; a list is a dictionary whose keys happen to be 0..n-1; array_is_list is the official 8.1 health check; json_encode emits a JSON array for lists, a JSON object for dictionaries; an empty [] encodes as [] or {} depending on options - the classic two-faces pitfall.
Python: list/dict are first-class types (example); json.dumps behaves like PHP but can NEVER surprise - the type is fixed at declaration; "checking listness" is a non-question in Python, which is exactly the root difference between one dynamic container and two static types.
Rust: Vec vs HashMap (example); serde emits array/object by type; same philosophy as Go - the language never offers one face with two skins.
TypeScript: runtime same as JS Array.isArray (example); at compile time string[] and Record<string, T> are already apart - the two faces cannot hide in TS's static layer.