Current Location: Home> Latest Articles> How to Output PHP Array Key-Value Pairs Using the print Function: A Simple and Practical Guide

How to Output PHP Array Key-Value Pairs Using the print Function: A Simple and Practical Guide

gitbox 2025-10-01

1. Using the print Function to Output Array Key-Value Pairs

First, get familiar with the basic usage of the print function. In PHP, print is used to output the value of a string or variable. If you try to print an array directly using print, you will get a result like this:

<span><span><span class="hljs-variable">$myArray</span></span><span> = </span><span><span class="hljs-keyword">array</span></span><span>(
    </span><span><span class="hljs-string">"name"</span></span><span> => </span><span><span class="hljs-string">"John"</span></span><span>,
    </span><span><span class="hljs-string">"age"</span></span><span> => </span><span><span class="hljs-number">30</span></span><span>,
    </span><span><span class="hljs-string">"city"</span></span><span> => </span><span><span class="hljs-string">"New York"</span></span><span>
);
<p></span>print($myArray);<br>
</span>

This code will not output the array contents as expected; instead, it returns a string like the following:

<span><span><span class="hljs-title class_">Array</span></span><span>
</span></span>

At this point, we need other methods to extract each key-value pair from the array and output them in a suitable format.


2. Using print_r to Output Arrays

For arrays, PHP provides the print_r function, which prints the array structure and contents in a readable format:

<span><span><span class="hljs-variable">$myArray</span></span><span> = </span><span><span class="hljs-keyword">array</span></span><span>(
    </span><span><span class="hljs-string">"name"</span></span><span> => </span><span><span class="hljs-string">"John"</span></span><span>,
    </span><span><span class="hljs-string">"age"</span></span><span> => </span><span><span class="hljs-number">30</span></span><span>,
    </span><span><span class="hljs-string">"city"</span></span><span> => </span><span><span class="hljs-string">"New York"</span></span><span>
);
<p></span>print_r($myArray);<br>
</span>

The output will be:

<span><span>Array
(
    [</span><span><span class="hljs-meta">name</span></span><span>] => John
    [</span><span><span class="hljs-meta">age</span></span><span>] => </span><span><span class="hljs-number">30</span></span><span>
    [</span><span><span class="hljs-meta">city</span></span><span>] => New York
)
</span></span>

Although print_r clearly shows the array structure, if we want to print each key-value pair individually, a more manual approach is preferable.


3. Iterating Through an Array and Using print to Output Key-Value Pairs

If we want to output each key-value pair in a specific format, we can use a foreach loop to iterate through the array and print each pair. For example:

<span><span><span class="hljs-variable">$myArray</span></span><span> = </span><span><span class="hljs-keyword">array</span></span><span>(
    </span><span><span class="hljs-string">"name"</span></span><span> => </span><span><span class="hljs-string">"John"</span></span><span>,
    </span><span><span class="hljs-string">"age"</span></span><span> => </span><span><span class="hljs-number">30</span></span><span>,
    </span><span><span class="hljs-string">"city"</span></span><span> => </span><span><span class="hljs-string">"New York"</span></span><span>
);
<p></span>foreach ($myArray as $key => $value) {<br>
print("Key: " . $key . " | Value: " . $value . \n");<br>
}<br>
</span>

The output will be:

<span><span><span class="hljs-section">Key: name | Value: John</span></span><span>
</span><span><span class="hljs-section">Key: age | Value: 30</span></span><span>
</span><span><span class="hljs-section">Key: city | Value: New York</span></span><span>
</span></span>

This approach allows us to output each key-value pair line by line while controlling the output format.


4. Formatting Array Key-Value Output

To make the output more visually appealing and easier to read, you can format each line using the sprintf function. For example:

<span><span><span class="hljs-variable">$myArray</span></span><span> = </span><span><span class="hljs-keyword">array</span></span><span>(
    </span><span><span class="hljs-string">"name"</span></span><span> => </span><span><span class="hljs-string">"John"</span></span><span>,
    </span><span><span class="hljs-string">"age"</span></span><span> => </span><span><span class="hljs-number">30</span></span><span>,
    </span><span><span class="hljs-string">"city"</span></span><span> => </span><span><span class="hljs-string">"New York"</span></span><span>
);
<p></span>foreach ($myArray as $key => $value) {<br>
print(sprintf("Key: %-10s | Value: %-20s\n", $key, $value));<br>
}<br>
</span>

The output will now have a more organized layout:

<span><span><span class="hljs-section">Key: name       | Value: John               </span></span><span>
</span><span><span class="hljs-section">Key: age        | Value: 30                 </span></span><span>
</span><span><span class="hljs-section">Key: city       | Value: New York           </span></span><span>
</span></span>

By adjusting the numbers in %-10s and %-20s, you can control the column width of key-value pairs to achieve the desired output format.


5. Conclusion

This article introduced how to use PHP’s print function to output array key-value pairs. We can iterate through the array using a foreach loop and use print to output each key-value pair individually. While print_r can directly print the entire array, sometimes we need more control over the output of each element, making the foreach loop with print a practical solution.

With proper formatting, the output can be clearer and more organized, making debugging and reading easier. In real-world development, applying these methods flexibly according to needs can significantly improve code readability and debugging efficiency.