PHP, lesson 7
Most languages have separate types for lists and dictionaries. PHP has just one, the array, and it does both jobs. Under the hood every PHP array is an ordered map: a list of keys, each pointing at a value, kept in the order you added them.
Write a list with square brackets. PHP gives the elements integer keys starting at 0:
Reading a key that doesn't exist gives a warning and null, so check first when you're not
sure.
Useful everyday functions:
Pass true as the third argument of in_array() to compare strictly with ===.
explode() goes the other way, cutting a string into an array:
explode(" ", "72 95 88") gives ["72", "95", "88"]. Remember the pieces are strings; use
or a loop to convert them.
2 exercises to try what you just read.
array_map('intval', ...)sort() sorts from low to high, rsort() from high to low. Both change the array in
place and renumber the keys:
array_slice($array, 0, 3) returns the first three elements as a new array, leaving the
original alone.
Give your own keys with => and the array becomes a lookup table, also called an
associative array:
isset() returns false both for missing keys and for keys whose value is null;
array_key_exists() checks only whether the key is there. The ?? operator is the neat way
to read a key that may be missing.
Keys are integers or strings. A string that looks like a whole number, such as "8",
becomes the integer 8, so $a["8"] and $a[8] are the same element.
The key => value form works for lists too, where the key is the position.
print_r() and var_dump() print a whole array, which is great while debugging.
One line of input holds race scores separated by spaces (at least one). Print the best three scores from highest to lowest on one line, separated by single spaces. With fewer than three scores, print them all.
Sample input:
Sample output:
The first line is a number n. The next n lines each hold a name and a phone number
separated by a space. Every line after that is a name to look up: print its number, or
not found. If a name is listed twice, the later number replaces the earlier one.
Sample input:
Sample output:
<?php
$scores = [72, 95, 88];
$scores[] = 60; // append: [72, 95, 88, 60]
echo $scores[0], "\n"; // 72
echo count($scores), "\n"; // 4
echo $scores[count($scores) - 1], "\n"; // 60, the last element<?php
$scores = [72, 95, 88];
var_dump(in_array(95, $scores, true)); // bool(true): is the value there?
array_unshift($scores, 100); // add to the front
$last = array_pop($scores); // remove and return the last element
echo implode(", ", $scores), "\n"; // join values into one string
echo array_sum($scores), " ", max($scores), "\n";<?php
$scores = [72, 95, 88];
rsort($scores);
print_r($scores); // [95, 88, 72]<?php
$prices = ["tea" => 2.5, "cake" => 3.0];
$prices["juice"] = 2.0; // add or overwrite
echo $prices["tea"], "\n"; // 2.5
unset($prices["cake"]); // remove a key
var_dump(isset($prices["milk"])); // bool(false)
var_dump(array_key_exists("tea", $prices)); // bool(true)
echo $prices["milk"] ?? 0, "\n"; // 0: a fallback instead of a warning<?php
foreach ($scores as $score) {
echo $score, "\n";
}
foreach ($prices as $item => $price) {
echo "$item costs $price\n";
}72 95 88 60 9195 91 882
ana 555-1234
ben 555-9876
ben
cara555-9876
not found