What Are Indexed And Associative Arrays in PHP?
Variables are not doubt very efficient in storing temporary data. But each variable can only store one value at a time. So if you are handling a large number of data Arrays are the best option you have to store and manipulate it. We will be discussing Simple arrays (or indexed arrays) and the Associative arrays in this tutorial. Also, the “for each” loop will be discussed since we skipped it last time.
Simple or Indexed Arrays
In PHP, we can simply create a new array using this function “array();”. You need to add all the values inside this function separated by a comma and finally store the whole function to a variable e.g. $first_Array=array(11,22,44,88,100);
In this example given above, we created a new array with values of 11, 22, 44, 88 and 100. And gave this array a name of $first_Array. Now why this array called an indexed array is because of the reason that we access all the data inside it using indexes. Index of every array starts from 0. Given is the syntax of accessing an element in an array.
$arrayName[Index];
Let’s say we want to print the first value of $firstArray. We will have to write “echo $first_Array[0]”. This will print the first value i.e. 11. Accessing other elements will be like
The output of this will be
One useful function that you just need to remember is count(); It tells the whole length of an array. So in order to make it loop through the whole array, this function is really handy. For example
Now, this loop will output all of the entries in an array regardless of the number of entries available in it.
Associative Arrays
These are some special kind of arrays that are used to store a title along with a value. We can access any value by mentioning the title of that entry instead of an index. Let’s look at their syntax
$assoc_Array= array(“title1”=>”value”, “title2”=>”value”,…). Here is an example
So the value of these titles will be printed as
For Each Loop
Last time we skipped this topic as it is purely related to arrays and the best logical way was to discuss it with the arrays. It is really handy in looping through an array of any kind. Here is all you need to know
It is similar to for loop with a minor difference. Syntax to loop through an indexed array is simple
foreach($arrayName as $temp ){
code;
}
Here, the temp will hold each value from the array one by one and run the code inside it. We can use the value of temp on each iteration for our purpose. Let’s print the value of an array using this loop.
In this example, the variable $a will access the values of $arr and inside the code, we will print them. Here is the output
There is also another version of this loop to access the values of an associative array. In this loop we mention
Foreach($arr as $title => $value)
Since there are titles for each value in an associative array, hence we make a variable to hold the value of it as well on each iteration. A real example is
Indexed and Associative Arrays in PHP
And the output of this is
We will end this tutorial here. Keep sending us your feedback on these tutorials. For next time we will discuss Functions in PHP. Stay connected!
Part One: HOW TO RUN YOUR FIRST PHP SCRIPT ON WINDOWS
Part Two: BASICS OF HTML5 ON THE WAY TO PHP LEARNING
Part Three: PHP WORKING WITH DATA TYPES AND VARIABLES
Part Four: IF ELSE STATEMENTS AND ARITHMETIC OPERATIONS IN PHP
Part Five: TUTORIAL ON TYPES OF LOOPS IN PHP – PHP LOOP TYPES
Part Seven: FUNCTIONS IN PHP
Part Eight: INTRODUCTION TO MYSQL
Part Nine:CONNECTING MYSQL DATABASE PHP
Part Ten: FORMS IN PHP – PHP FORM EXAMPLE WITH DATABASE
Main Image Source : Pixabay
Also See : Why is BitCoin Outperforming Stocks?