array_is_list

array_is_list(mixed $value): bool

判断数组是否为「真列表」(0..n-1 顺序整数键);PHP 数组两副面孔的体检工具——json_encode 对两副面孔输出数组或对象全看它。

C C23

C23
#include <stdio.h>

/* C 数组天然就是列表:连续、下标 0..n-1。
   「字典」需要手写结构——这个区分由你自建的类型系统承担 */
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

C++26
#include <iostream>
#include <map>
#include <string>
#include <vector>

int main() {
    // 列表/字典之分是静态的: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

v11.0.100
using System.Collections.Generic;

class MainClass {
    static void Main() {
        // List<T> 与 Dictionary<K,V> 是不同类型——无需检查
        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

v1.27.4
package main

import "fmt"

func main() {
	// 切片与映射是不同类型——PHP 数组的「两副面孔」
	// 在 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

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

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"]——数组面孔
console.log(JSON.stringify(dict)); // {"x":1,"y":2}——对象面孔
输出
true
false
["a","b","c"]
{"x":1,"y":2}

PHP v8.6.0

v8.6.0
$list = ["a", "b", "c"];           // 0..n-1 顺序整数键
$dict = ["x" => 1, "y" => 2];       // 字符串键:字典面孔
$odd  = [1 => "one", 0 => "zero"];  // 同样的键值对,乱序

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:键序破坏列表性
echo json_encode($list), "\n"; // ["a","b","c"]          ——数组面孔
echo json_encode($dict), "\n"; // {"x":1,"y":2}        ——对象面孔
echo json_encode($odd), "\n";  // {"1":"one","0":"zero"}——保留插入序
输出
true
false
false
["a","b","c"]
{"x":1,"y":2}
{"1":"one","0":"zero"}

Python v3.15.2

v3.15.2
import json

lst = ["a", "b", "c"]
dct = {"x": 1, "y": 2}

print(type(lst).__name__)  # list——一等公民类型
print(type(dct).__name__)  # dict
print(json.dumps(lst))     # ["a", "b", "c"]——数组面孔
print(json.dumps(dct))     # {"x": 1, "y": 2}——对象面孔
输出
list
dict
["a", "b", "c"]
{"x": 1, "y": 2}

Rust v1.99.1

v1.99.1
// Vec 与 HashMap:两个独立类型,由编译器区分
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

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
// 编译期同样把两者分开:
const first: string = list[0];
console.log(first); // a
输出
true
false
a

语言差异说明

C:数组天然是列表(示例)——连续存储 + 整数下标是语言契约;「字典」要自建哈希表,两副面孔在 C 里是两套数据结构。

C++:std::vector vs std::map 强类型区分(示例);注意 std::map 按键排序遍历、std::unordered_map 才对应 PHP 的哈希语义。

C#:List<T>/Dictionary<K,V> 类型即语义(示例);C# 数组 T[] 也是天然 list;JSON 序列化行为与 PHP/Python/JS 一致(列表出数组、字典出对象)。

Go:slice 与 map 是两个类型(示例 %T 展示);Go 的 map 遍历乱序——比 PHP 更「字典」;json.Marshal 同样按类型出数组/对象。

Java:List/Map 接口家族(示例);instanceof 判型;Jackson/Gson 同样按类型决定 JSON 形态;Java 数组转 List 用 Arrays.asList 但定长——「列表性」在 Java 是编译期事实。

JavaScript:Array.isArray 对应(示例);JSON.stringify 按类型出数组/对象——与 PHP json_encode 行为同构,但 JS 永远不会出现「数组却编出对象」的意外,因为类型不混。

PHP:核心教学(示例三态 + json 对照)——PHP 只有一种数组,list 是「恰好键为 0..n-1」的字典;array_is_list 是 8.1 的官方体检;json_encode 对 list 出 JSON 数组、对字典出 JSON 对象;空数组 [] 编码形态取决于 $options(默认 [])——两副面孔的经典坑。

Python:list/dict 是一等类型(示例);json.dumps 行为与 PHP 同构但永不意外——类型在声明时已定;「PHP 的 list 体检」在 Python 是伪问题,这正是动态单容器 vs 静态双类型的根本差异。

Rust:Vec vs HashMap(示例);serde 序列化按类型出数组/对象;与 Go 同哲学——语言不给你「一张脸两张皮」的机会。

TypeScript:运行期同 JS Array.isArray(示例);编译期 string[] 与 Record<string, T> 已分开——两副面孔在 TS 的静态层无处遁形。