Core\Arr::isList
Tests whether an array is a plain list.
An array is a list when its keys are exactly 0, 1, 2 and so on, with no key missing and no key out of order. An array you build with [], or by adding entries with $a[] = ..., is a list. An array with names as keys is not, and neither is one whose numbers start at 1 or skip a number.
An array with no entries is a list.
Good to know: this is the test that says what an array will look like in JSON. A list is written as a JSON array, and every other array is written as a JSON object.
Parameters
$aarray<T>The array to test.
Return value
Returns bool. true for a list and for the empty array. false when any key is out of sequence, or is not its index written exactly in decimal digits.
Examples
A list and an array with names
<?nvs// Shows how to test whether an array is a plain list.
array<string> $colours = ['red', 'green', 'blue'];
// The keys are 0, 1 and 2, so this is a list. This prints yes.echo Core\Arr::isList($colours) ? 'yes' : 'no', "\n";
array<string> $user = ['name' => 'Ada', 'city' => 'Wien'];
// The keys are names, so this is not a list. This prints no.echo Core\Arr::isList($user) ? 'yes' : 'no', "\n";yesnoThe order of the keys matters
<?nvs// Shows what makes an array stop being a list. The keys have to be 0, 1, 2// and so on, and they have to be put in in that order.
array<string> $backwards = [];$backwards[1] = 'second';$backwards[0] = 'first';
// Both keys are there, but 1 was put in before 0. This prints no.echo Core\Arr::isList($backwards) ? 'yes' : 'no', "\n";
array<string> $empty = [];
// An array with no entries at all is a list. This prints yes.echo Core\Arr::isList($empty) ? 'yes' : 'no', "\n";noyesWhat json makes of it
<?nvs// Shows how a program checks what an array will look like in JSON. A list// becomes a JSON array, and any other array becomes a JSON object.
array<string> $colours = ['red', 'green'];array<string> $user = ['name' => 'Ada'];
// This prints ["red","green"].echo Core\Arr::isList($colours) ? 'array: ' : 'object: ', Core\Json::encode($colours), "\n";
// This prints {"name":"Ada"}.echo Core\Arr::isList($user) ? 'array: ' : 'object: ', Core\Json::encode($user), "\n";array: ["red","green"]object: {"name":"Ada"}