1--TEST-- 2Test array_is_list() function 3--FILE-- 4<?php 5 6function test_is_list(string $desc, $val) : void { 7 try { 8 printf("%s: %s\n", $desc, json_encode(array_is_list($val))); 9 } catch (TypeError $e) { 10 printf("%s: threw %s\n", $desc, $e->getMessage()); 11 } 12} 13 14test_is_list("empty", []); 15test_is_list("one", [1]); 16test_is_list("two", [1,2]); 17test_is_list("three", [1,2,3]); 18test_is_list("four", [1,2,3,4]); 19test_is_list("ten", range(0, 10)); 20 21test_is_list("null", null); 22test_is_list("int", 123); 23test_is_list("float", 1.23); 24test_is_list("string", "string"); 25test_is_list("object", new stdClass()); 26test_is_list("true", true); 27test_is_list("false", false); 28 29test_is_list("string key", ["a" => 1]); 30test_is_list("mixed keys", [0 => 0, "a" => 1]); 31test_is_list("ordered keys", [0 => 0, 1 => 1]); 32test_is_list("shuffled keys", [1 => 0, 0 => 1]); 33test_is_list("skipped keys", [0 => 0, 2 => 2]); 34 35$arr = [1, 2, 3]; 36unset($arr[0]); 37test_is_list("unset first", $arr); 38 39$arr = [1, 2, 3]; 40unset($arr[1]); 41test_is_list("unset middle", $arr); 42 43$arr = [1, 2, 3]; 44unset($arr[2]); 45test_is_list("unset end", $arr); 46 47$arr = [1, "a" => "a", 2]; 48unset($arr["a"]); 49test_is_list("unset string key", $arr); 50 51$arr = [1 => 1, 0 => 0]; 52unset($arr[1]); 53test_is_list("unset into order", $arr); 54 55$arr = ["a" => 1]; 56unset($arr["a"]); 57test_is_list("unset to empty", $arr); 58 59$arr = [1, 2, 3]; 60$arr[] = 4; 61test_is_list("append implicit", $arr); 62 63$arr = [1, 2, 3]; 64$arr[3] = 4; 65test_is_list("append explicit", $arr); 66 67$arr = [1, 2, 3]; 68$arr[4] = 5; 69test_is_list("append with gap", $arr); 70 71?> 72--EXPECT-- 73empty: true 74one: true 75two: true 76three: true 77four: true 78ten: true 79null: threw array_is_list(): Argument #1 ($array) must be of type array, null given 80int: threw array_is_list(): Argument #1 ($array) must be of type array, int given 81float: threw array_is_list(): Argument #1 ($array) must be of type array, float given 82string: threw array_is_list(): Argument #1 ($array) must be of type array, string given 83object: threw array_is_list(): Argument #1 ($array) must be of type array, stdClass given 84true: threw array_is_list(): Argument #1 ($array) must be of type array, true given 85false: threw array_is_list(): Argument #1 ($array) must be of type array, false given 86string key: false 87mixed keys: false 88ordered keys: true 89shuffled keys: false 90skipped keys: false 91unset first: false 92unset middle: false 93unset end: true 94unset string key: true 95unset into order: true 96unset to empty: true 97append implicit: true 98append explicit: true 99append with gap: false 100