Arrays in PHP
Building, growing, slicing, and sorting PHP's list-shaped arrays.
<?php
$langs = ["php", "ruby", "go"];
$langs[] = "rust";
echo $langs[0], " through ", $langs[count($langs) - 1], PHP_EOL;
echo count($langs), " languages", PHP_EOL;
// array_slice copies; sort reorders the array in place
print_r(array_slice($langs, 1, 2));
sort($langs);
echo implode(", ", $langs), PHP_EOL;
How it works
$langs[] =appends without naming an index;countsizes the array.array_slicecopies a range and leaves the original untouched.sortreorders in place;implodejoins the result into one string.
Keywords and builtins used here
array_slicecountechoimplodeprint_rsort
The run, in numbers
- Lines
- 11
- Characters to type
- 309
- Tokens
- 80
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 60 seconds.
Step 1 of 3 in Arrays, step 7 of 27 in Language basics.