Arrays in PHP are ordered maps.
A map is a data structure that associates a key to a value.
Arrays are versatile and can be treated as a linear array (like in C
or Java
), list [vector], hash-table, dictionary, collection, stack, or a queue.
A key is optional, but if specified must either be an int
or a string
.
When a key is not provided, PHP defaults to integer keys in increasing order, starting at 0
.
$no_keys = ["my", "first", "array"];
$integer_keys = [0 => "my", 1 => "first", 2 => "array"];
$no_keys == $integer_keys // => equal returns true
$no_keys === $integer_keys // => strictly equal returns true
An array can store values of all types. Each value can have a different type.
Arrays can be declared as a literal (written in code, as done above) or created and manipulated as functions. Access, assign, append values using the index operator:
$prime_numbers = [2, 3, 5, 6];
$prime_numbers[3] = 7; // replace 6 with 7
$prime_numbers[] = 11; // array now contains [2, 3, 5, 7, 11]
Function arguments can be specified such that it can take any number of arguments:
<?php
function actOnItems(...$items) {
// $items is an array containing 0 or more values
// used to call the function.
}
actOnItems(1, 2, 3, 4); // $items => [1, 2, 3, 4]
In this exercise you need to implement some functions to manipulate a list of programming languages.
Define the language_list
function that takes no arguments and returns an empty list.
<?php
language_list();
// => []
Modify the language_list
function, so it takes a variadic argument of languages (strings).
It should return the resulting list with the languages in the list.
<?php
$language_list = language_list();
// => []
$language_list = language_list("Clojure", "PHP");
// => ["Clojure", "PHP"]
$language_list = language_list("PHP", "Haskell", "Java", "C++", "Rust")
// => ["PHP", "Haskell", "Java", "C++", "Rust"]
Define the add_to_language_list
function that takes 2 arguments, an array of languages, and the new language.
<?php
$language_list = language_list();
// => []
$language_list = add_to_language_list($language_list, "Clojure");
// => ["Clojure"]
Define the prune_language_list
function to remove the first language from the array of languages.
<?php
$language_list = language_list("PHP");
// => ["PHP"]
$language_list = prune_language_list($language_list);
// => []
Define the current_language
function that takes 1 argument (a language list).
It should return the first language in the list.
Assume the list will always have at least one item.
<?php
$language_list = language_list("PHP", "Prolog");
// => ["PHP", "Prolog"]
$first = current_language($language_list);
// => "PHP"
Define the language_list_length
function that takes 1 argument (a language list).
It should return the number of languages in the list.
<?php
$language_list = language_list("PHP", "Prolog", "Wren");
// => ["PHP", "Prolog", "Wren"]
language_list_length($language_list);
// => 3
Sign up to Exercism to learn and master PHP with 11 concepts, 110 exercises, and real human mentoring, all for free.