YogeshChauhan . com

Can We Use For Loop to Loop Through Associative Arrays in PHP?

in PHP on December 6, 2019

We just learned about the 3 types of arrays in PHP from the following blog post.

3 Types of Arrays in PHP

Let's take a look at the examples of how to loop through each type of arrays.

Indexed Array Loop Through Using For Loop


$hobbies= array("Reading", "Sports", "Guitar");
    $arraylength = count($hobbies);
    echo "List of Hobbies";
      for($x = 0; $x < $arraylength; $x++) {
          echo $hobbies[$x];
      }

Output:


List of Hobbies:
Reading
Sports
Guitar

So as we can see in the example above, we can easily loop through indexed array using for loop.

But for Associative Arrays we need to use ForEach loop.

Let's take a look at the example.


$age = array("Yogi"=>"28", "Hulk"=>"56", "Captain"=>"34");

foreach($age as $x => $x_value) {
    echo "Name=" . $x . ", Age=" . $x_value;
    echo "<br>";
}

Output:


Name=Yogi, Age=28
Name=Hulk, Age=56
Name=Captain, Age=34

So, foreach will give us the correct output. If you try to use for loop, you can use only one variable and if you try to use for loop inside a for loop then it will become 2 dimensional array but it's actually not and you'll get an error.

Let's take a look at the last type.

We are using the same data from this blog post.

3 Types of Arrays in PHP


$water = array
  (
    array("Members Mark",22,18),
    array("Dasani",15,13),
    array("Bubly",5,2),
    array("Aqua",17,15)
  );
    
for ($row = 0; $row < 4; $row++) {
  echo "<p><b>Row: $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 3; $col++) {
    echo "<li>".$water[$row][$col]."</li>";
  }
  echo "</ul>";
}

Output:


Row: 0

Members Mark
22
18

Row: 1

Dasani
15
13

Row: 2

Bubly
5
2

Row: 3

Aqua
17
15

As you can see in the multidimensional array example above, we need for loop inside for loop to fetch all the data. The more deep the data is stores, the more for loops we will need and it'll become very complex data storage. 

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 7 How to disable right click and drag and drop of images using jQuery? May 7 How to render Lists in React? May 7 What’s the difference between variables in CSS and SCSS (Sass)? May 7 How to define variables in SCSS (Sass)? May 7 How to show and hide an element on click in React? May 5 Use inline if to make a shorter conditional syntax in React

You might also like these

3 Common Usability Mistakes In Web DesignUI/UXHow to create a Child Theme in WordPress?WordPressHow to change style of all direct children of an element using jQuery?jQuerybin2hex() and chr() String Functions in PHPPHPPre-defined DatePipe format options in Angular 9AngularWhat happened to your Google Analytics Tracking ID, can’t find it?Misc