How to Traverse an array in PHP and print?

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, or adding up each element of an array of prices. 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.

Example:-

<!DOCTYPE html>
<html>
	<body>
        <?php
            $array = array("Lux","Sampoo");

            foreach($array as $value) {
              print  " $value";
            }
        ?>
	</body>
</html>

Output:-

Lux Shampoo

Example:-

<!DOCTYPE html>
<html>
	<body>
        <?php
            $fnames = ["Amit", "Rahul", "Vijay", "Ravi", "Roshan"];

            $arrObject = new ArrayObject($fnames);
            $arrayIterator = $arrObject->getIterator();
                    
            while( $arrayIterator->valid() ){
                echo $arrayIterator->current() . "<br />";
                $arrayIterator->next();
            }
        ?>
	</body>
</html>

Output:-

Related Posts

Associative Array in PHP?

An associative array is a simple and very important array of PHP. This array is also used the most, it is very easy to understand this array,…

How to work in functions PHP?

If there is any task in your program which you train to execute repeatedly then instead of writing the code for that task at a different place…

For Loofs in PHP

In for-loop initialization, condition and increment happen on the same line. The loop keeps on repeating as long as the condition is true. When the for loop…

If Else Conditionals in Php?

If Else statement is the most important feature of any programming language, PHP is used to run any Code Of Block according to If Else conditions. This…

What are operators in PHP? PHP – Operator Types

Operators are used to developing data. In Programming Languages, Operators are taken from Mathematics and Operator Programmers work with Data. An operator is a chain of symbols…

String Functions in PHP?

When many characters are written together in sequence, they form a string. For example, India is a string. It is made up of i, n, d, i,…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x