OSPHP.004: Arrays — Indexed Arrays, Associative Arrays, Nested Arrays, Access, Update, and Iteration

Dark neon programming workstation visualizing indexed, associative, and nested array structures on large monitors.

Key Takeaways

  • PHP arrays store multiple values in one variable.
  • Keys can be integers or strings. That lets one PHP array act like a list, a key-value map, or a mixture of both.
  • Square brackets [] are the modern array syntax.
  • foreach is the normal beginner-friendly way to iterate through array values or key/value pairs.
  • Arrays can contain other arrays. Those are nested or multidimensional arrays.

OSPHP.003 introduced variables: named references to values that can change while a script runs. Arrays build on that idea by letting one variable hold a collection of related values. PHP’s official array documentation describes a PHP array as an ordered map whose keys may be integers or strings. That design is more flexible than the fixed, purely numeric arrays beginners may know from some other languages.

Dani Krossing introduces indexed, associative, and multidimensional arrays in modern PHP.

Create an Indexed Array

<?php
$miners = ["S21", "S19", "A15"];

var_dump($miners);
  • $miners[0] is "S21".
  • $miners[1] is "S19".
  • $miners[2] is "A15".
  • If you omit explicit keys, PHP generates integer keys starting at 0.
echo $miners[0]; // S21
$miners[1] = "S19 XP";
$miners[] = "M60";

Associative Arrays Use Named Keys

An associative array uses meaningful string keys instead of relying only on numeric positions. The => operator connects each key to its value. This is useful when the key itself explains what the data represents, such as "model", "hashrate", or "online". Access works with the key in square brackets, and assigning to an existing key replaces that value. PHP does not treat indexed and associative arrays as two unrelated data types; both are the same underlying array type.

Dani Krossing demonstrates associative arrays and named key/value access in PHP.
$miner = [
    "model" => "S21",
    "hashrate" => 200,
    "online" => true,
];

echo $miner["model"];
$miner["hashrate"] = 205;
$miner["efficiency"] = 17.5;

Nested Arrays Store Structured Data

An array element can itself be another array. That makes nested arrays useful for rows of records, configuration data, API responses, shopping carts, inventory lists, and other structured information. To reach a nested value, add another pair of square brackets for each level. A beginner should read the expression from left to right: choose the outer record first, then the key inside that record.

Dani Krossing explains multidimensional arrays—arrays stored inside other arrays.
$fleet = [
    ["model" => "S21", "hashrate" => 200],
    ["model" => "S19 XP", "hashrate" => 140],
    ["model" => "M60", "hashrate" => 172],
];

echo $fleet[1]["model"]; // S19 XP

Iterate With foreach

The PHP foreach documentation defines two common forms: one gives you each value, and the other gives you both the key and value. This is usually clearer than manually managing an index when you simply need to visit every array element. PHP also supports iterating through nested arrays by placing one loop inside another or by destructuring the inner records.

Traversy Media’s PHP beginner course covers arrays, array functions, loops, and foreach in a working PHP environment.
$models = ["S21", "S19 XP", "M60"];

foreach ($models as $model) {
    echo $model . "\n";
}
$miner = [
    "model" => "S21",
    "hashrate" => 200,
    "online" => true,
];

foreach ($miner as $key => $value) {
    echo $key . ": ";
    var_dump($value);
}

Useful Beginner Operations

  • Read: $array[$key]
  • Update: $array[$key] = $newValue;
  • Append: $array[] = $newValue;
  • Count: count($array)
  • Remove: unset($array[$key]);
  • Check a key: array_key_exists($key, $array)
  • Check a value: in_array($value, $array, true)
$models = ["S21", "S19 XP"];
$models[] = "M60";

echo count($models); // 3

unset($models[1]);

Integer Keys Do Not Automatically Renumber

$models = ["S21", "S19 XP", "M60"];
unset($models[1]);

var_dump($models);
  • Key 0 still exists.
  • Key 1 has been removed.
  • Key 2 still exists.
  • Use array_values($models) if you specifically want new sequential integer keys.

Mixed Keys Are Legal

$data = [
    0 => "first",
    "status" => "online",
    7 => "seventh-key",
];
  • PHP arrays may contain integer and string keys at the same time.
  • This flexibility is powerful, but predictable structures are usually easier to read and maintain.
  • For record-like data, consistently named string keys are normally clearer.

Common Beginner Mistakes

  • Assuming the first numeric index is 1: automatically generated integer keys normally begin at 0.
  • Using an undefined key: read only keys you know exist, or check first.
  • Mixing up = and =>: = assigns a variable or array element; => associates a key with a value inside array syntax and some iteration syntax.
  • Expecting unset() to renumber keys: it removes the chosen element but preserves the other keys.
  • Building deeply nested arrays without clear names: use readable keys and consistent structure.
  • Changing an array while iterating by reference without understanding references: leave reference-based iteration for a later lesson.

Practice

  1. Create arrays.php.
  2. Create an indexed array with four technology names.
  3. Print the first and third elements.
  4. Replace the second element.
  5. Append a fifth element with [].
  6. Create an associative array representing one server with hostname, ip, and online keys.
  7. Update the IP address.
  8. Use foreach to print every key and value.
  9. Create an array containing three server arrays and print the hostname of the second server.
  10. Remove one indexed element and inspect the remaining integer keys with var_dump().

Knowledge Check + Answers

  1. What can a PHP array key be? An integer or string.
  2. What key does the first automatically indexed value normally receive? 0.
  3. What operator connects a key to a value in array syntax? =>.
  4. How do you append a value using modern array syntax? $array[] = $value;.
  5. How do you get the number of elements? count($array).
  6. What loop is designed to traverse array values and key/value pairs? foreach.
  7. What is a nested array? An array stored as a value inside another array.
  8. Does unset() automatically renumber numeric keys? No.

Previous PHP Lessons

Next PHP Lesson

  • OSPHP.005: Control Flow — if, elseif, else, comparisons, and choosing which code path runs.

BitcoinVersus.Tech Editor’s Note:
We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please donate using the QR code provided

Bitcoin donation QR code for 5,000 sats.

For best QR-code scanning results, please scan this donation code from a desktop or larger screen. Mobile screens may prevent camera recognition.

BitcoinVersus.Tech content is provided for informational and educational purposes.

Leave a Reply