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

Modernizing Software Delivery in the Dutch Tech Landscape: A Pragmatic DevOps Playbook

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 Qualified AWS DevOps Trainers: Enterprise Playbook

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

Teaching Failure: How to Build Incident Response and Resilience into DevOps Training

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 Open Road in Japan: An Independent Rider’s Handbook for Cross-Country Touring

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

A Comprehensive Guide to Scooters, Motorcycles, and Road Trips

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

Monitoring and Observability Training: Complete Curriculum Guide

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
0
Would love your thoughts, please comment.x
()
x