You can either add multiple properties by adding multiple elements within a sub-array

$foodArray = [['name' => 'Apple', 'color' => 'Yellow'], 
              ['name' => 'Banana', 'color' => 'yellow']];

foreach($foodArray as $fruit) {
    echo $fruit['name']." - ".$fruit['color']." <br />";
}

Or if you just need these two properties, you can use the key as the name, and the value as the color.

$foodArray = ['Apple' => 'green', 'Banana' => 'yellow'];
foreach($foodArray as $fruit => $color) {
    echo $fruit." - ".$color ." <br />";
}
  • Live demo at https://3v4l.org/jGePl
Answer from Qirel on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ control-structures.foreach.php
PHP: foreach - Manual
But If you iterate by reference using foreach ($arr as &$v) then $arr is turned into a reference and you can change it during iteration Foreach WILL LOOP through new values added to the array while inside the loop. <?php $a = [1, 2, 3]; foreach ($v as &$v) { echo $v; if ($v === 2) { $v[] = 4; } } ?> Output: 1234 Foreach WILL NOT LOOP through values deleted from the array while inside the loop.
๐ŸŒ
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.
Discussions

Loop Through An Array In PHP with 2 or more properties - Stack Overflow
I'm trying to create an array with two properties and loop through it. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do you look through a 2D array in PHP using a while loop
Edit; missed the "using a while loop", so updating my answer to reflect the actual requirement. Alright, so - there are a couple of options, but the basic principle could look something like this: Name Amount Price More on reddit.com
๐ŸŒ r/PHPhelp
8
1
April 5, 2022
Loop through array while vs foreach [Noob Question]
The answer is that the answer is too complicated to give. No, one is not more performent or memory efficient in all cases. Each will be "more" efficient in a certain realm. Also, notice that they are not doing the same thing. One is destructively iterating backwards over the array, and the other is simply iterating forwards. If I could give you one piece of advice, it would be to avoid optimization in cases like this. In fact, avoid it in most cases. My thoughts on optimization: On my blog . Just use what's more readable (the foreach), and be done with it. It's not worth your time, or anyone else's to worry about the performance of this... More on reddit.com
๐ŸŒ r/PHP
7
3
July 9, 2013
bindParam (loop through array?)
Use question mark placeholders to bind and pass an array of values directly to execute(). No need to bind in foreach. $possibleValues = array('v', 'b', 'c'); $in = rtrim(str_repeat('?,', count($possibleValues)), ','); $sql = "SELECT something FROM table WHERE column IN ($in)"; $stmt = $pdo->prepare($sql); $stmt->execute($possibleValues); More on reddit.com
๐ŸŒ r/PHP
10
4
December 6, 2011
๐ŸŒ
Koladechris
koladechris.com โ€บ blog โ€บ how-to-loop-through-arrays-in-php
How to Loop Through Arrays in PHP
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.
๐ŸŒ
Parthpatel
parthpatel.net โ€บ home โ€บ php โ€บ 6 ways to loop through an array in php
6 ways to loop through an array in php | Parth Patel - a Web Developer
September 11, 2021 - The condition will be to continue fetching element from an array til our index values is less than the count of array (or length of the given array). Since, while loop will not increment our index variable automatically, we need to increment ...
๐ŸŒ
Hacking with PHP
hackingwithphp.com โ€บ 5 โ€บ 3 โ€บ 0 โ€บ the-two-ways-of-iterating-through-arrays
The two ways of iterating through arrays: list(), each(), and foreach loops โ€“ Hacking with PHP - Practical PHP
Generally speaking, using foreach loops is the most optimised way to loop through an array, and is also the easiest to read. In practice, however, you will find foreach loops and list()/each() loops in about equal proportions, despite the latter option being slower.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-iterating-through-an-array-in-php
How to Iterate Over an Array in PHP? - GeeksforGeeks
July 23, 2025 - In PHP, `array_walk()` iterates through an array, applying a user-defined callback function to each element.
๐ŸŒ
Zero To Mastery
zerotomastery.io โ€บ blog โ€บ php-foreach-loop-explained
Introduction To The Foreach Loop In PHP (With Code Examples) | Zero To Mastery
When combined with a foreach loop, it can be especially useful for iterating through arrays where each element itself is an array, and you want to assign each sub-array's elements to variables for easier access.
๐ŸŒ
FlatCoding
flatcoding.com โ€บ home โ€บ php foreach loop: how to access keys & values in arrays
PHP foreach Loop: How to Access Keys & Values in Arrays - FlatCoding
June 30, 2025 - You can use foreach when you want to go through each value in an array or object. Other loops like for, while, or do...while do not need arrays. They use conditions or counters. You do not set a start or end point.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-loop-through-an-array-using-a-foreach-loop-in-php
How to Loop Through an Array using a foreach Loop in PHP? - GeeksforGeeks
July 23, 2025 - The foreach loop iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively. The foreach loop works for both indexed and associative arrays.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-foreach-loop
PHP foreach Loop - GeeksforGeeks
September 5, 2024 - The foreach loop can iterate through each element easily. ... Associative arrays contains the array elements in (key, value) pair format. The foreach loop can handle both keys and values, making it ideal for working with associative arrays.
๐ŸŒ
Code.mu
code.mu โ€บ en โ€บ php โ€บ faq โ€บ php-loop-through-array
PHP Array Loop
Looping through an array in PHP is done using the foreach command. Click to view details and examples of how it works.
๐ŸŒ
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.
๐ŸŒ
Flexiple
flexiple.com โ€บ php โ€บ php-foreach
PHP foreach() loop for indexed and associative arrays - Flexiple
<?php $freelancer = array( "name" => "Eric", "email" => "Eric@gmail.com", "age" => 22, "gender" => "male" ); // Loop through employee array foreach($freelancer as $key => $value) { echo $key . ": " . $value .
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ php-array-handbook
PHP Array Handbook โ€“ How to Create, Work with, and Loop Through Arrays
January 21, 2025 - PHP provides several built-in functions for manipulating arrays and several ways to loop through arrays.
๐ŸŒ
Dr. Balvinder Taneja
drbtaneja.com โ€บ home โ€บ looping array using each() and foreach() loop
Looping array using each() and foreach() loop - Dr. Balvinder Taneja
May 11, 2024 - In this example, the foreach loop iterates through each element in the $fruits array, and the $fruit variable takes on the value of each element in turn.
๐ŸŒ
Matt Doyle
elated.com โ€บ home โ€บ blog โ€บ using foreach to loop through php arrays
Using foreach to Loop Through PHP Arrays
July 23, 2022 - For example, you may want to display each value in an HTML table, or give each element a new value. In Counting PHP Array Elements Using count(), I showed how you can use a for loop along with the count() function to loop through an array.