To print multiple array values in PHP, first, we have to merge multiple arrays and store them in a variable.
To merge two arrays we use array_merge()
function. Here on this task we check two types of multiple arrays and merge those to print the complete value.
- Example with indexed arrays.
- And with associative arrays.
Example With Indexed Arrays
Here we define two different arrays, to get the combined result we first merge both arrays by using the PHP array_merge()
function.
After that, we loop through the combined array variable to get complete data.
Also Read: Bootstrap Datepicker- Complete Solution to Integrate
1 2 3 4 5 6 7 8 9 10 11 |
<?php $colors = array("Red", "Green", "Blue", "Yellow", "Orange"); $names = array("John", "Jerry", "Aron", "Amit", "Jesse"); $combineArray = array_merge($colors,$names); // Loop through combine array foreach($combineArray as $value){ echo $value . "<br>"; } ?> |
Output:
Also Read: CSS background-image Properties
Example With Associative Arrays
In the below example, we define two associative arrays and combine them by using the same PHP function. After that, we loop through the array to print all values.
And then we print a complete array with their keys and values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $colors = array("colorOne" => "Red", "colorTwo" => "Green", "colorThree" => "Blue"); $name = array("nameOne" => "John", "nameTwo" => "Doe", "nameThree" => "Joe", "nameFour" => "Jesse"); $combinedArray = array_merge($colors, $name); // Loop through combined array foreach($combinedArray as $value){ echo $value . "<br>"; } //print complete array echo "<pre>"; print_r($combinedArray); ?> |
Output:
Conclusion
The above source code is the complete explanation of How we can print multiple array values in PHP at once by merging the arrays.
To merge both types of arrays indexed and associative we use PHP inbuilt function array_merge()
.
You can consider the complete program as a PHP array merge with an example.
Please let me know if you are facing any issues with the implementation.
Also Read: XAMPP, Apache – Error: Apache shutdown unexpectedly
Happy Coding..!