Programming Lessons

[PHP]Let’s Try Using Associative Arrays![Programming for Kids and Above #5]


I’m kat, the manager of ‘kat’s blog.

This time, which is the fifth installment of the ‘Programming Lessons’ series, I will be talking about associative arrays.

Last time on the fourth article, we learned about arrays, in which you can put in multiple values, but this time we will be learning about associative arrays which are even more easier to use.

It is used a lot in programming, so let’s try to remember it!

What we will use this time

programming language

This time we will use the following programming language to write out a program.


programming environment

Let’s use the paiza website which is listed below for executing the program.

paiza website

For those who are using this website for the first time, the way to use it is listed in the page below, so please check it out.

How to use[Basics][Programming with Your Browser][PHP]Hello, I'm kat, the manager of 'kat's blog.' This time I would like ...

Reviewing arrays

Last time I mentioned that arrays are like a container that can hold multiple values. For example, the first value could be ‘apple,’ and the second value could be ‘orange.’

Also, I explained that arrays each have a number (starting from 0), and each of them are called a ‘key.’

In the case of arrays, because the keys are automatically attached, you do not need to enter them in the source code.

Now then, let’s review the source code of arrays.

 $array = ['apple', 'orange', 'banana', 'strawberry']; // Automatically the keys 「0」「1」「2」「3」are attached
 echo $array[1];

When you run the above source code, keys are automatically attached, so ‘orange‘ which has the key of ‘1’ should be displayed.

Arrays are used a lot when creating groups of things that are the same kind.

For instance, a group of fruit (apple, orange, banana, etc.), a group of animals (dog, cat, elephant, etc.), or a group of vehicles (car, train, airplane, etc.).

What are associative arrays?

Just like arrays, associative arrays are a container which you can enter multiple values.

However, the difference between arrays are that you can name the keys as you like.

Looking at the example above, in the key titled ‘name,’ there is ‘Bobby,’ and in the ‘age’ key there is the value ’10.’

In this example, the information of a person has been put together as an associative array.

Because the keys are letters instead of numbers, you can understand the information just by looking at the keys.

Now then, let’s take a look at the source code of an associative array.

 $user = [
  'name' => 'Bobby',
  'age' => '10',
  'gender' => 'male',
  'favorite color' => 'blue'
 echo $user['gender'];

If you run the source code above, the ‘male‘ value which is in the ‘gender’ key will be displayed.

As shown, associative arrays are useful to keep together the information of people and things.

For instance, product (name of product, price, expiration date, etc.) ,etc.

Final thoughts

Thank you for reading to the end of this lesson this time as well.

In this lesson we covered arrays and associative arrays. It isn’t a matter of which is better, and it is important to be able to use whichever matches your purpose best.

Arrays and associative arrays always pop up when doing programming, so let’s make sure to remember them!

See you next time!

プログラマー歴7年、2歳の子供を持つパパです。 興味のあることはプログラミングや今後のIT技術などです。 趣味でオンラインカードゲームのサイトを運営しております。 プログラミングを通して社会に貢献していきたいです。