The Double Arrow Operator and Multidimensional Arrays - Images: Japanese
Hey everyone, hope you're doing well!
Last time we covered associative arrays. This time we're continuing with arrays — specifically, we'll look at the double arrow operator that shows up when defining associative arrays in PHP, and use it as a stepping stone to explore multidimensional arrays.
Let's start with how to write the double arrow operator. It looks like this: '=>'. You've already seen array() in the arrays lesson. Up until now, the examples on this site have only used array() for indexed arrays — but you can actually use it for associative arrays too. That's where the double arrow operator comes in.
The double arrow operator is common in newer programming languages, but you won't find it in older ones, so it's considered a relatively modern syntax. There's also a similar-looking operator called the 'arrow operator' (->), but that one is used with classes — so we'll put it aside for now.
One thing to keep in mind: in Google Search, symbols can trigger special search behavior.
Because of that, searching for => or -> directly in Google often doesn't give you useful results.
Search engines just aren't great at handling symbol-only queries.
So make sure you remember the names — double arrow operator and arrow operator — when you need to look them up.
Here's a sample defining an associative array using array().
<?php
$vocaloids = array('miku' => '080-xxxx-1234');
// This is equivalent to writing: $vocaloids['miku'] = '080-xxxx-1234'
Inside the parentheses of array(), use => to map a key to its value. By the way, if you're on PHP 5.4 or later, you can also use [] instead of array().
<?php $vocaloids = ['miku' => '080-xxxx-1234']; // PHP 5.4+ allows [] in place of array()
Here's a sample with multiple entries. Just like with indexed arrays, separate each entry with a comma. The indentation style below is a common convention — take note of it.
<?php
$vocaloids = array(
'miku' => '080-xxxx-1234',
'IA' => '090-yyyy-1234',
'rin' => '080-zzzz-1234'
);
var_dump($vocaloids);
Running this produces the following output.
array(3) {
["miku"]=>
string(13) "080-xxxx-1234"
["IA"]=>
string(13) "090-yyyy-1234"
["rin"]=>
string(13) "080-zzzz-1234"
}
This is equivalent to writing it like this:
<?php $vocaloids['miku'] = '080-xxxx-1234'; $vocaloids['IA'] = '090-yyyy-1234'; $vocaloids['rin'] = '080-zzzz-1234'; var_dump($vocaloids);
That's how the double arrow operator works — it behaves a bit like the assignment operator =. A good way to remember it: the double arrow operator is used inside the arguments of array() to map keys to values.
One thing to watch out for: even though => looks like it points to the right, the assignment actually flows right-to-left. In programming, the right-hand side gets evaluated first and assigned to the left — and => works the same way. Don't mix this up!
Now let's talk about multidimensional arrays. A multidimensional array is simply an array that contains other arrays inside it. This isn't PHP-specific — pretty much every language supports nesting arrays within arrays. The name "multidimensional array" sounds pretty cool, honestly — a little edgy, even — and personally I like it.
Defining a multidimensional array is straightforward: just put another array() or [] inside an array element, and you've got yourself a multidimensional array. The syntax is simple, but the downside is that your source code quickly fills up with array() and [], making it tricky to read. Let's take a look.
<?php $arr = [ ['miku', 'IA'], ['Naked Snake', 'Kazuhira Mirror'] ]; var_dump($arr);
See what I mean? It's already a bit chaotic with all those brackets. Here's what the output looks like.
array(2) {
[0]=>
array(2) {
[0]=>
string(4) "miku"
[1]=>
string(2) "IA"
}
[1]=>
array(2) {
[0]=>
string(11) "Naked Snake"
[1]=>
string(15) "Kazuhira Mirror"
}
}
Still chaotic. But that's nothing yet. Once you start defining multidimensional associative arrays with array(), things get seriously tangled.
<?php $arr = array( 'METAL GEAR SOLID' => array( 'HERO' => 'Solid Snake' ), 'METAL GEAR SOLID 2' => array( 'HERO' => 'Raiden' ), 'METAL GEAR SOLID 3' => array( 'HERO' => 'Naked Snake' ), 'METAL GEAR SOLID 4' => array( 'HERO' => 'Old Snake' ), 'METAL GEAR SOLID 5' => array( 'HERO' => 'BIG BOSS' ) ); var_dump($arr);
Output:
array(5) {
["METAL GEAR SOLID"]=>
array(1) {
["HERO"]=>
string(11) "Solid Snake"
}
["METAL GEAR SOLID 2"]=>
array(1) {
["HERO"]=>
string(6) "Raiden"
}
["METAL GEAR SOLID 3"]=>
array(1) {
["HERO"]=>
string(11) "Naked Snake"
}
["METAL GEAR SOLID 4"]=>
array(1) {
["HERO"]=>
string(9) "Old Snake"
}
["METAL GEAR SOLID 5"]=>
array(1) {
["HERO"]=>
string(8) "BIG BOSS"
}
}
Quite the sight, isn't it? Multidimensional arrays tend to produce messy-looking code, and they can be genuinely hard to parse until you get used to them.
That said, PHP is a server-side language, and you'll often be pulling data from databases or JSON — and that data usually comes back as a multidimensional array. Being able to read through those structures without too much trouble is an important skill.
It might feel overwhelming at first, but it gets easier with practice. Just push through it. Also, when writing associative arrays, always double-check your commas and semicolons — missing one is a common source of errors.
Now let's look at how to access values inside a multidimensional array. Since the structure is arrays nested within arrays, you just chain [] together to navigate through the levels until you reach the value you want.
Here's a multidimensional array defined around METAL GEAR SOLID V: The Phantom Pain.
<?php
$MGS5TPP = array(
'Title' => 'METAL GEAR SOLID V: The Phantom Pain',
'Developer(s)' => 'Kojima Productions',
'Producer' => 'Hideo Kojima',
'Main Characters' => [
'BIG BOSS',
'Kazuhira Mirrer',
'Revolver Ocelot',
'Quiet',
'Skull Face'
]
);
Let's practice with this. First, let's retrieve the value 'Kojima Productions'. It's stored under the key 'Developer(s)' in the $MGS5TPP array, so we access it like this.
<?php
$MGS5TPP = array(
'Title' => 'METAL GEAR SOLID V: The Phantom Pain',
'Developer(s)' => 'Kojima Productions',
'Producer' => 'Hideo Kojima',
'Main Characters' => [
'BIG BOSS',
'Kazuhira Mirrer',
'Revolver Ocelot',
'Quiet',
'Skull Face'
]
);
echo $MGS5TPP['Developer(s)']; // Outputs the string 'Kojima Productions'
Now let's retrieve the value 'BIG BOSS'. It's the element at index 0 inside the 'Main Characters' array, which itself is inside $MGS5TPP. So we write it like this.
<?php
$MGS5TPP = array(
'Title' => 'METAL GEAR SOLID V: The Phantom Pain',
'Developer(s)' => 'Kojima Productions',
'Producer' => 'Hideo Kojima',
'Main Characters' => [
'BIG BOSS',
'Kazuhira Mirrer',
'Revolver Ocelot',
'Quiet',
'Skull Face'
]
);
echo $MGS5TPP['Main Characters'][0]; // Outputs the string 'BIG BOSS'
That's the pattern: trace through the multidimensional array structure by chaining [] with the appropriate key or index at each level. Updating a value works the same way.
<?php
$MGS5TPP = array(
'Title' => 'METAL GEAR SOLID V: The Phantom Pain',
'Developer(s)' => 'Kojima Productions',
'Producer' => 'Hideo Kojima',
'Main Characters' => [
'BIG BOSS',
'Kazuhira Mirrer',
'Revolver Ocelot',
'Quiet',
'Skull Face'
]
);
$MGS5TPP['Main Characters'][0] = 'Naked Snake'; // Replaces 'BIG BOSS' with 'Naked Snake'
echo $MGS5TPP['Main Characters'][0]; // Outputs the string 'Naked Snake'
We covered count() earlier as a way to get the number of elements in an array. By default, count() only counts the top-level (first dimension) of an array.
<?php
$MGS5TPP = array(
'Title' => 'METAL GEAR SOLID V: The Phantom Pain',
'Developer(s)' => 'Kojima Productions',
'Producer' => 'Hideo Kojima',
'Main Characters' => [
'BIG BOSS',
'Kazuhira Mirrer',
'Revolver Ocelot',
'Quiet',
'Skull Face'
]
);
echo count($MGS5TPP); // Only counts the top level: 'Title', 'Developer(s)', 'Producer', 'Main Characters' — so 4
To count all elements across all dimensions of a multidimensional array, pass 1 or COUNT_RECURSIVE as the second argument to count(). The same applies to sizeof().
<?php
$MGS5TPP = array(
'Title' => 'METAL GEAR SOLID V: The Phantom Pain',
'Developer(s)' => 'Kojima Productions',
'Producer' => 'Hideo Kojima',
'Main Characters' => [
'BIG BOSS',
'Kazuhira Mirrer',
'Revolver Ocelot',
'Quiet',
'Skull Face'
]
);
echo count($MGS5TPP, COUNT_RECURSIVE); // Counts all elements recursively — result is 9
// echo count($MGS5TPP, 1); // Passing 1 as the second argument works too
// echo sizeof($MGS5TPP, COUNT_RECURSIVE); // sizeof() accepts the same syntax
So that's multidimensional arrays for you. All those array() calls and brackets can make your head spin at first, but keep at it and it'll start to click. You've got this.
In the next article we'll be looking at boolean values — the ones that deal strictly in true or false — along with comparison operators. See you then!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.