Table of Contents
How to Store Loop Values in a PHP Array
There are 3 steps to storing loop values in PHP arrays. You need to instantiate a PHP variable with an empty array. Then, you will run your requirements with a loop like foreach. Finally, you can check for any needed conditions and add a value to the newly instantiated array. Check out the remaining article for examples.
Are you needing to store values from a loop in a new array? This article will break down some common scenarios that I have experienced throughout my programming career.
Storing Foreach Loop Values In Array PHP Code Example
<?php
// Define an indexed array
$numbers = [5, 4, 2, 6, 10];
// Define an empty array to store the resulting values
$results = [];
// Loop through the array using foreach loop
// store any 2, 6, 10 values in the new array to validate results
foreach ($numbers as $number) {
// Add a modified value to the results array
// run a check on the value
if (in_array($number, [2,6,10])) {
$results[$number] = true;
}
}
// Output the resulting array
print_r($results);
Article Highlights
- The foreach loop is commonly used to iterate through arrays because it allows for key + value variables.
- Instantiate a PHP result array before running your loop
- Storing values from associative arrays can be tricky but still is doable with foreach loops.

Table of Contents
In this article, we will cover the following topics.
- Using the foreach loop to store values
- Iterating Through an Indexed Arra.
- Iterating Through Associative Array.
- Iterating Through Dynamic Arrays.
Using the foreach Loop with PHP
The foreach loop in PHP is commonly used to parse array and object variables. The foreach loop is useful because you can loop through an unknown sized array and associate the key & value of the elements with unique variables. This is When the total number of iterations of the loop is not defined, it is better to use a foreach loop. The number of iterations of this loop depends on the number of array elements or the number of properties of the object used in the loop for reading values.
Syntax:
Here’s the syntax if you want to iterate over an array’s values only. The $value gets the value one by one as the foreach loops through an array.
foreach($iterable as $value)
{
//foreach loop BODY
}
Here’s the syntax if you want to iterate over an array’s values as well as keys. Here the $key gets the key, and the $value gets the value one by one as the foreach loops through the array.
foreach(iterable as $key => $value)
{
//foreach loop BODY
}
Storing Values in Array from a foreach Loop PHP Code Example
In the following PHP code, we first define an array called $numbers with some values. Then we define an empty array called $values to store the loop values.
Next, we use a foreach loop to loop through the $numbers array and assign each value to the variable $number. We use the [] syntax inside the loop to add each value to the $values array.
Finally, we use the print_r() function to output the contents of the $values array, which should contain the same values as the $numbers array.
<?php
// Define an array
$numbers = array(5, 25, 50, 400, 500);
// Define an empty array to store the loop values
$values = array();
// Loop through the array using foreach
foreach ($numbers as $number) {
// Store each value in the new array
$values[] = $number;
}
// The Output the new array
print_r($values);
?>
Output:
Array
(
[0] => 5
[1] => 25
[2] => 50
[3] => 400
[4] => 500
)
As we can see in the output, it first stores the loop values in the array and returns them in the results.
Store the length of each string in an array using a foreach loop
In the below PHP code, we first define an array called $strings with some strings. Then we define an empty array called $lengths to store the length of each string.
Next, we use a foreach loop to loop through the $strings array and assign each string to the variable $string. Inside the loop, we use the strlen() function to get the length of each string and use the [] syntax to add each length to the $lengths array.
Finally, we use the print_r() function to output the contents of the $lengths array, which should contain the lengths of each string in the $strings array.
Storing array value string length PHP code example
<?php
// Define an array of strings
$strings = array("Oranges", "Banana", "Apple", "Mangos");
// Define an empty array to store the string lengths
$lengths = array();
// Loop through the array using foreach
foreach ($strings as $string) {
// Store the length of each string in the new array
$lengths[] = strlen($string);
}
// Output the new array
print_r($lengths);
?>
Output:
Array
(
[0] => 7
[1] => 6
[2] => 5
[3] => 6
)
Store the sum of each array in an array using a foreach loop
In the following PHP code, we first define an array called $arrays with some arrays. Then we define an empty array called $sums to store the sum of each array.
Next, we use a foreach loop to loop through the $arrays array and assign each array to the variable $array. Inside the loop, we use the array_sum() function to get the sum of each array and use the [] syntax to add each sum to the $sums array.
Finally, we use the print_r() function to output the contents of the $sums array, which should contain the sums of each array in the $arrays array.
These are just a couple of examples, but you can use the foreach loop to store any value in an array, depending on your specific needs.
Store Sum of Array With Loop PHP Code Example
<?php
// Define an array of arrays
$arrays = [
[10, 2, 4],
[4, 3, 10],
[10, 10, 10]
];
// Define an empty array to store the array sums
$sums = [];
// Loop through the array using foreach
foreach ($arrays as $array) {
// Store the sum of each array in the new array
$sums[] = array_sum($array);
}
// Output the new array
print_r($sums);
?>
Output:
Array
(
[0] => 16
[1] => 17
[2] => 30
)
Iterating Through an Indexed Array with a Foreach Loop
In this section, we will iterate through an indexed array and add values to the resulting array using a foreach loop in PHP.
In the below PHP code, we first define an indexed array called $numbers with some values. Then, we define an empty array called $results to store the resulting values of the loop.
Next, we use a foreach loop to loop through the $numbers array and assign each value to the variable $number. Inside the loop, we modify the value of the $number by multiplying it by 2 and use the [] syntax to add the modified value to the $results array.
Finally, we use the print_r() function to output the contents of the $results array, which should contain the modified values of the $numbers array.
<?php
// Define an indexed array
$numbers = array(5, 4, 2, 6, 10);
// Define an empty array to store the resulting values
$results = array();
// Loop through the array using foreach
foreach ($numbers as $number) {
// Add a modified value to the results array
$results[] = $number * 2;
}
// Output the resulting array
print_r($results);
?>
Output:
Array
(
[0] => 10
[1] => 8
[2] => 4
[3] => 12
[4] => 20
)
Iterating Through Associative Array with a Foreach Loop
In this section, we will iterate through an associative array and add values to the resulting array using a foreach loop in PHP.
In the following PHP code, we first define an associative array called $grades with some key-value pairs. Then, we define an empty array called $results to store the resulting values of the loop.
Next, we use a foreach loop to loop through the $grades array and assign each key-value pair to the variables $name and $grade. Inside the loop, we modify the value of $grade by adding 5 to it and use the $name variable as the key to store the modified value in the $results array.
Finally, we use the print_r() function to output the contents of the $results array, which should contain the modified values of the $grades array with the same keys.
<?php
// Define an associative array
$grades = array(
"John" => 90,
"Mark" => 85,
"Joel" => 65,
"Jordan" => 80
);
// Define an empty array to store the resulting values
$results = array();
// Loop through the array using foreach
foreach ($grades as $name => $grade) {
// Add a modified value to the results array
$results[$name] = $grade + 5;
}
// Output the resulting array
print_r($results);
?>
Output:
Array
(
[John] => 95
[Mark] => 90
[Joel] => 70
[Jordan] => 85
)
Iterating Through Dynamic Array with a Foreach Loop
In this section, we will iterate through a dynamic array and add values to the resulting array using a foreach loop in PHP.
In the below PHP code, we first define a dynamic array called $fruits with some values. Then, we define an empty array called $results to store the resulting values of the loop.
Next, we use a foreach loop to loop through the $fruits array and assign each value to the variable $fruit. Inside the loop, we modify the value of $fruit by concatenating it with some text and adding it to the $results array using the [] syntax.
Finally, we use the print_r() function to output the contents of the $results array, which should contain some modified values based on the original values of the $fruits array.
Iterating Dynamic Array with foreach Loop PHP Code Example
<?php
// Define a dynamic array
$fruits = array("apple", "banana", "orange");
// Define an empty array to store the resulting values
$results = array();
// Loop through the array using foreach
foreach ($fruits as $fruit) {
// Add a modified value to the results array
$results[] = "I love " . $fruit . "s!";
}
// Output the resulting array
print_r($results);
?>
Output:
Array
(
[0] => I love apples!
[1] => I love bananas!
[2] => I love oranges!
)
How to Store Loop Value in PHP Arrays
We can use the foreach loop ($array_nam as $elem) to iterate over elements of an indexed array. We can also use the foreach loop ($array_name as $key => $val) to iterate over elements of an associative array in PHP.
The foreach() returns an error if you use it on variables with a different data type. The foreach() loop does not modify the values of the internal pointer.
This article explains storing loop values in an array using the foreach loop in PHP. It also describes some PHP code examples of storing loop values in the arrays using the foreach loop.
This article also focuses on how to store loop values in the indexed, associative, and dynamic arrays using the foreach loop in PHP, and we hope you find this article helpful.
Learn More About Loops in PHP
Want to learn how to use loops in PHP? This article is part of our series on looping through PHP arrays. Check out our full series to become a PHP array loop ninja.
- Intro to PHP Array Loops
- When to use while vs foreach loop in PHP
- How to Use array_chunk with foreach loop
- How to break out of a foreach loop
- Loop Through Multidimensional Arrays
- Difference between while and do while loops
- Recursive Loop Multidimensional JSON Arrays
- Store foreach Loop Value in Array
Learn the Fundamentals of Good Web Development
Please take a moment and sign up for our free email course on the fundamentals of good web development. Every week is packed with a roundup of articles on our site and from around the web, where we go deep into developing exceptional web applications. We have meetups, code reviews, slack chats, and more.