$array = array("Jonathan","Sampson");

foreach($array as $value) {
  print $value;
}

or

$length = count($array);
for (i < $length; $i++) {
  print $array[$i];
}
Answer from Sampson on Stack Overflow
🌐
W3Schools
w3schools.com › PHP › php_looping_foreach.asp
PHP foreach loop
The PHP foreach loop - Loops through a block of code for each element in an array or each property in an object.
🌐
PHP
php.net › manual › en › control-structures.foreach.php
PHP: foreach - Manual
*/ for (size_t i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) { /* In each iteration, the memory location of 8 is going to be dereferenced and set to arr[i]. As a result, the last value will be equal to the value before it */ *current = arr[i]; printf("Current:%d\n", *current); } return 0; } Just as PHP's $value remains a reference to the last element after a by-reference foreach, current here remains a pointer to arr[3]. Every iteration of the second loop writes into arr[3], ultimately copying the second-to-last value onto the last element. ... Definitely relevant for PHP 7+ 1. You can't change array during iteration Foreach WILL NOT LOOP through new values added to the array <?php while inside the loop.
Discussions

php - I'm trying to create an array with a foreach loop, but the array only stores the last item - WordPress Development Stack Exchange
Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes. Closed 9 years ago. ... More on wordpress.stackexchange.com
🌐 wordpress.stackexchange.com
February 10, 2017
php looping through two dimensional array using for loop
Abe Layee is having issues with: I am trying to loop through my array and display the user name and password for each person, but i keep getting undefined offset More on teamtreehouse.com
🌐 teamtreehouse.com
4
February 9, 2017
Trying to access PHP 2D array elements with a for loop?

foreach ($categories as $key => $value) { //do something }

Check this for details https://www.php.net/manual/en/control-structures.foreach.php

More on reddit.com
🌐 r/webdev
6
0
December 19, 2019
How to loop this JSON object from php?
First of all, you have an error in your JSON data. Here is the fixed one: { "data": [ { "section": "Technology", "weighting": "0.15", "questions": [ { "id": "1", "weighting": "0.25", "score": "5" }, { "id": "2", "weighting": "0.25", "score": "5" } ] }, { "section": "Data", "weighting": "0.15", "questions": [ { "id": "1", "weighting": "0.25", "score": "5" }, { "id": "2", "weighting": "0.25", "score": "5" } ] } ] } I, then, created a file 'data.json' and 'test.php'. Here is 'test.php': '; } } It prints: 0.25 0.25 0.25 0.25 As you see, you made two mistakes: Your JSON data was corrupted and you were looping the whole array, but you have to loop array['data']. Hope it helps. P.S.: Get better in debugging PHP with print_r and echoes. Really helps. More on reddit.com
🌐 r/PHPhelp
11
7
August 23, 2020
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-foreach-loop
PHP foreach Loop - GeeksforGeeks
September 5, 2024 - The foreach loop in PHP is a powerful and convenient way to iterate over arrays and objects. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively.
🌐
Zero To Mastery
zerotomastery.io › blog › php-foreach-loop-explained
Introduction To The Foreach Loop In PHP (With Code Examples) | Zero To Mastery
May 29, 2024 - The PHP foreach function is a powerful tool for iterating over arrays or objects, providing a more intuitive and cleaner approach than traditional for or while loops, especially when working with collections.
Find elsewhere
🌐
Koladechris
koladechris.com › blog › how-to-loop-through-arrays-in-php
How to Loop Through Arrays in PHP
September 14, 2024 - The unique thing you need to do is to get the length of the array with the count() function, then echo out each element of the array by accessing each element with the iteration variable you set. ... Remember any HTML in your PHP file is the template for that PHP file.
🌐
Simplilearn
simplilearn.com › home › resources › software development › guide to understand loop in php
PHP Loop: For, ForEach, While, Do While [With Example] | Simplilearn
September 9, 2025 - Loop in PHP is an Iterative Control Structure to be executed until a certain condition is met. Learn more about PHP For, ForEach, While, Do While loop Now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
W3Schools
w3schools.com › php › php_looping_for.asp
PHP for loop
The PHP for loop - Loops through a block of code a specified number of times.
🌐
Matt Doyle
elated.com › home › blog › using foreach to loop through php arrays
Using foreach to Loop Through PHP Arrays
July 23, 2022 - Here’s an example that loops through an associative array of movie information, displaying each element’s key and value inside an HTML definition list: $movie = array( "title" => "Rear Window", "director" => "Alfred Hitchcock", "year" => 1954, "minutes" => 112 ); echo "<dl>"; foreach ( $movie as $key => $value ) { echo "<dt>$key:</dt>"; echo "<dd>$value</dd>"; } echo "</dl>";
🌐
Buka Knowledge
knowledge.buka.sh › understanding-php-arrays-and-how-to-use-foreach-loops
Understanding PHP Arrays and How to Use foreach Loops
October 24, 2024 - The foreach loop goes through each element of the array, assigning it to the variable $lang one by one. The as keyword tells PHP to create a copy of each array element and store it in $lang temporarily for the duration of that iteration.
🌐
The Man in the Arena
carlalexander.ca › php-array-functions-instead-loops
How to use PHP array functions instead of loops | The Man in the Arena
September 11, 2016 - They let you traverse an array and perform any operation that you wish on each array element. That said, it’s easy to overuse loops. When that happens, your code becomes hard to read and to test. That’s because loops, while easy to use, can also make your code much more complex. But, lucky for us, PHP has a wealth of array functions.
🌐
Serious Code Words
schwern.net › home › blog › loop through an array in php
Loop through an array in PHP - Serious Code Words
March 24, 2026 - The for structure in PHP has the following form: for (initial_assignment, condition, increment) { ... } The idea is to iterate one by one until we have gone through all the elements. Thus, if we use a counter variable, the condition will be to not have reached the index of the last element.
🌐
DEV Community
dev.to › chrisrhymes › the-php-for-loop-47ch
The PHP for loop - DEV Community
September 13, 2021 - Also consider using a foreach loop for looping through an array as this will loop over the length of the array without calculating the length. Many examples use $i as a variable in for loops, such as the php.net docs (also this article, oops), ...
🌐
Laracasts
laracasts.com › discuss › channels › laravel › need-to-push-dynamic-values-in-array-with-foreach-loop-php
Need to push dynamic values in array with foreach loop php
$peopleData_Nearby = array( 'user_id' => array(), 'trip_id' => array(), 'first_name' => array(), 'last_name' => array(), 'distance' => array() ); This loop will be executed x no of time depending on db result · foreach ($All_Trips_Data as $all_trip) { $user_id = $all_trip->user_id; $trip_ID ...
🌐
Team Treehouse
teamtreehouse.com › community › php-looping-through-two-dimensional-array-using-for-loop
php looping through two dimensional array using for loop (Example) | Treehouse Community
February 9, 2017 - In your second loop: for ($k =0; $k<=$i; $k++) { it does not make sense to compare $k directly to $i. You want to compare $k to the size of the $array at $i so: for ($k = 0; $ <= count($array[$i]); $k++) { 8,378 Points · Abe Layee · 8,378 Points February 10, 2017 11:18pm · i changed it but nothing is printing out on the screen · 4,642 Points · Wes Grant · 4,642 Points February 13, 2017 2:43pm · Oh yeah... sorry... My PHP skills are a little rusty but I got a REPL up and running and hashed this out for you...
🌐
Copahost
copahost.com › home › php loop through array
PHP Loop through array - Copahost
May 31, 2020 - The foreach function is one of the most used for looping in PHP. As the name says: “For each” member, do such tasks. <?php foreach ($array as $key) { echo "Value: $key <br>"; } ?>