Arrays Flashcards
How do you add an element called Apple to the end of an array named ‘$fruits’.
array_push($fruits, ‘Apple’);
Using variable $lastFruit, how do you remove the last element from an array named ‘$fruits’ and return it?
$lastFruit = array_pop($fruits);
Using variable $firstFruit, how do you remove the first element from an array named ‘$fruits’ and return it?
$firstFruit = array_shift($fruits);
How do you add Mango and Pineapple to the beginning of an array named ‘$fruits’
array_unshift($fruits, ‘Mango’, ‘Pineapple’);
Using variable $combinedArray, how do you combine two arrays, ‘$array1’ and ‘$array2’ into a single array?
$combinedArray = array_merge($array1, $array2);
Using variable $reversedArray, how would you reverse the following array:
$fruits = [“Apple”, “Orange”, “Banana”, “Pear”];
$reversedArray = array_reverse($fruits);
Create an associate array named $capitals. It should contain the capital cities of “UK”, “France”, “Spain”, and “Wales”.
$capitals = [
“UK” => “London”,
“France” => “Paris”,
“Spain” => “Madrid”,
“Wales” => “Cardiff”
];