How to traverse array in PHP and print?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

the most common task with arrays is to do something with every element for instance, sending mail to each element of an array of addresses, updating each file in an array of filenames, there are several ways to traverse arrays in PHP and the one you choose will depend on your data and the task you’re performing.

foreach loop

foreach loop is use to iterate over arrays. For every counter of loop, an array element is assigned and the next counter is shifted to the next element.

Syntax = foreach (array_element as value)
           {
             //code to be executed
           }

Flow diagram

example1

<?php
		//  for index array 
			$subjects = [
			           "c++",
					   "php",
					   "java"
					   
					   ];
		    foreach($subjects as $value)
			{
				echo $value . "<br>";
			}
?>

output

c++ 
php 
java 

example2

<?php
                // for associative array
		$subjects = [
					"c++" => 24,
					  "php" => 25,
					  "java" => 26
					
					];
			foreach($subjects as $key => $value )
			{
				echo "$key = $value <br>";
			}				
?>

output

c++ = 24
php = 25
java = 26

Related Posts

Local DevOps Trainers Can Be Better for Hands-On Learning

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Navigating DevOps Trainer Selection for Hybrid Learning Environments

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

The Explorer’s Guide to Bhopal Tourism, Heritage Trails, and Lake Views

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Comprehensive Insights on Selecting Appropriate Medical Centers for Spine Health

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Complete Roadmap for Selecting the Best Commercial Pilot Training Schools

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Finding the Best Orthopedic Hospital Near Me for Complete Care

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x