Skip to content
Classroom

Array Macros

Built-in functions for working with arrays and lists.

Creating Arrays

array(value1, value2, ...)

Create an array from values.

$colors = array("red", "green", "blue");
$numbers = array(1, 2, 3, 4, 5);
$mixed = array("name" => "Alice", "age" => 25);  // Associative array

Array Shorthand (PHP 5.4+)

$colors = ["red", "green", "blue"];
$mixed = ["name" => "Alice", "age" => 25];

Array Access

$colors = ["red", "green", "blue"];

$first = $colors[0];   // Returns "red"
$second = $colors[1];  // Returns "green"
$last = $colors[2];    // Returns "blue"

Associative Arrays

$person = ["name" => "Alice", "age" => 25];

$name = $person["name"];  // Returns "Alice"
$age = $person["age"];    // Returns 25

Array Functions

count(array)

Return the number of elements.

$colors = ["red", "green", "blue"];
$total = count($colors);  // Returns 3

array_push(array, value)

Add element to end of array.

$numbers = [1, 2, 3];
array_push($numbers, 4);  // $numbers is now [1, 2, 3, 4]

array_pop(array)

Remove and return the last element.

$numbers = [1, 2, 3];
$last = array_pop($numbers);  // $last = 3, $numbers = [1, 2]

array_shift(array), array_unshift(array, value)

Remove from front or add to front.

$colors = ["red", "green", "blue"];
$first = array_shift($colors);     // $first = "red", $colors = ["green", "blue"]
array_unshift($colors, "yellow");  // $colors = ["yellow", "green", "blue"]

in_array(needle, array)

Check if value exists in array.

$colors = ["red", "green", "blue"];

if (in_array("red", $colors)) {
    // True - "red" is in the array
}

if (in_array("yellow", $colors)) {
    // False - "yellow" is not in the array
}

array_key_exists(key, array)

Check if a key exists in associative array.

$person = ["name" => "Alice", "age" => 25];

if (array_key_exists("name", $person)) {
    // True
}

if (array_key_exists("email", $person)) {
    // False
}

array_keys(array), array_values(array)

Get all keys or all values.

$person = ["name" => "Alice", "age" => 25];

$keys = array_keys($person);      // Returns ["name", "age"]
$values = array_values($person);  // Returns ["Alice", 25]

array_reverse(array)

Reverse the order of elements.

$numbers = [1, 2, 3, 4, 5];
$reversed = array_reverse($numbers);  // Returns [5, 4, 3, 2, 1]

array_unique(array)

Remove duplicate values.

$colors = ["red", "blue", "red", "green", "blue"];
$unique = array_unique($colors);  // Returns ["red", "blue", "green"]

array_slice(array, offset, length)

Extract a portion of the array.

$numbers = [1, 2, 3, 4, 5];

$subset = array_slice($numbers, 1, 3);  // Returns [2, 3, 4]
$last_two = array_slice($numbers, -2);  // Returns [4, 5]

array_merge(array1, array2, ...)

Combine multiple arrays.

$array1 = [1, 2, 3];
$array2 = [4, 5, 6];

$merged = array_merge($array1, $array2);  // Returns [1, 2, 3, 4, 5, 6]

Array Searching & Sorting

array_search(needle, array)

Find the key/index of a value.

$colors = ["red", "green", "blue"];
$index = array_search("green", $colors);  // Returns 1

sort(array), rsort(array)

Sort ascending or descending (reindexes numeric keys).

$numbers = [3, 1, 4, 1, 5, 9];
sort($numbers);   // Returns [1, 1, 3, 4, 5, 9]
rsort($numbers);  // Returns [9, 5, 4, 3, 1, 1]

asort(array), arsort(array)

Sort while preserving key associations.

$scores = ["Alice" => 90, "Bob" => 85, "Carol" => 92];
asort($scores);   // Sorts by value, keeps key => value pairs
// Results: ["Bob" => 85, "Alice" => 90, "Carol" => 92]

Examples

Random Selection

$animals = ["dog", "cat", "bird", "fish"];
$chosen = $animals[rand(0, count($animals) - 1)];
// Randomly selects one animal

Counting Occurrences

$letters = ["a", "b", "a", "c", "a", "b"];
$count = 0;

foreach ($letters as $letter) {
    if ($letter == "a") {
        $count++;
    }
}
// $count = 3

Building a List from Values

$numbers = [2, 4, 6, 8];
$list = "The even numbers are: " . implode(", ", $numbers);
// Returns "The even numbers are: 2, 4, 6, 8"

Removing Incorrect Options

$options = ["A", "B", "C", "D", "E"];

// Remove option C (incorrect)
$correct_options = array_values(array_diff($options, ["C"]));
// Returns ["A", "B", "D", "E"] (reindexed)

Looping Through Arrays

foreach

Iterate over array elements.

$colors = ["red", "green", "blue"];

foreach ($colors as $color) {
    echo $color . "\n";  // Prints each color
}

foreach with Associative Arrays

$person = ["name" => "Alice", "age" => 25, "city" => "Boston"];

foreach ($person as $key => $value) {
    echo $key . ": " . $value . "\n";
}
// Prints:
// name: Alice
// age: 25
// city: Boston

See Also