Multidimensional Arrays

A multidimensional array is an array that contains other arrays as its items. Think of it like a spreadsheet — rows and columns of data, where each row is itself an array.

Two-dimensional indexed arrays:

These use numbers for both levels. The outer array has numeric indexes, and each inner array also has numeric indexes. To access an item, you need two bracket pairs: $cars[0][1] means “first car array, second item inside it.”

$cars = [
  ["Volvo", 22, 18],
  ["BMW", 15, 13]
];
// $cars[0][0] = "Volvo"
// $cars[0][1] = 22

Two-dimensional associative arrays (the real-world version):

This is what you'll actually use in WordPress. The outer array can be indexed, but the inner arrays are associative with named keys:

$members = [
  ['name' => 'Alice', 'status' => 'active', 'joined' => 2021],
  ['name' => 'Bob', 'status' => 'expired', 'joined' => 2019]
];
// $members[0]['name'] = "Alice"
// $members[1]['status'] = "expired"

Or fully associative at both levels:

$users = [
  'user_123' => ['name' => 'Alice', 'email' => 'alice@example.com'],
  'user_456' => ['name' => 'Bob', 'email' => 'bob@example.com']
];
// $users['user_123']['email'] = "alice@example.com"

Accessing nested data:

You chain the brackets: first bracket gets you to the outer level, second bracket gets you inside. For indexed: $array[0][1]. For associative: $array['key1']['key2']. For mixed: $array[0]['name'].

Looping through multidimensional arrays:

You nest loops. The outer loop goes through the main array, the inner loop goes through each sub-array. With associative arrays, you use foreach for both levels.

Why This Matters:

Almost every complex WordPress function returns multidimensional arrays. get_users() returns an array of user objects (which are essentially associative arrays). MemberPress membership data? Array of member arrays. WooCommerce orders? Array of order arrays.