Sunday, December 15, 2024

PHP: Create an Array

An array is one of the fundamentals of almost any programming language. Whether you understand what it is or you just need to learn the syntax, the process of creating arrays in PHP is relatively straightforward.

I am going to show you how to create two different types of arrays:

Indexed Array – an array in which each element is referenced by a numerical index
Associative Array – an array in which each element is referenced by a string index (sometimes referred to as a hash map/table)

Although you can also create one more type of array in PHP known as a Multidimensional array (which is a fancy word for an array within an array), we will focus only on setting up the two types listed above.

Array function in PHP

The PHP function to create an array variable is simply:

array()

This function takes a list of values and creates an array containing those values. It can create both an indexed array and an associative array, depending on the arguments you pass into it.

Creating an Indexed array

To create an indexed array, we simply pass in our data into the array() function, and starting at the first reference, it is given an index of 0.

$oS = array("Windows", "Mac", "Linux", "Ubuntu");

In the code above, I created a simple indexed array of different operating systems and saved this array in the variable oS.

Now, to reinforce again in this indexed array, each element is referenced by a numerical number. This also starts at 0, instead of 1. Therefore, what we have is the following:

Windows[0] Mac[1] Linux[2] Ubuntu[3]

You must not forget that in PHP our referenced integers will start at 0.

Creating an Associative array

An associate array, as explained above, is a more detailed type of array, where each element is referenced by an actual string of text. This allows for many more possibilities down the road and can potentially help organize things even better.

To create an associative array, we use the array() function again but input different arguments:

$oS = array( "name" => "Microsoft Windows",
"year" => 1970,
"creator" => "Bill Gates"
"version" => 7.0 );

In the array above, the string of text to the left of the => is the reference for its preceding data input. Each element is not referred to numerically. If we were to visualize it numerically, it would look something like this:

name[0] year[1] creator[2] version[3]

However, we must think of associative arrays as strings (because it is what they are, after all) in order to utilize them to the best of our advantage. If I know I want the name of the OS in my $oS variable, I know to pull out the “name” index to get that. If I want the version, I know to pull out the “version” index. This is much more handy than trying to memorize numbered indexes.

Chris Luongo
Chris Luongo
Chris is a self-taught web designer and developer out of Atlanta, GA. This geek enjoys coffee, cold brews, bike riding, and twisting any form of tech into submission.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!