There are 4 ways to output an array in PHP: print_r() prints the array in a human-readable way var_dump() prints the array in a detailed way, showing the type and value echo() can output the array but does not format json_encode () Convert the array to a JSON string
The method of outputting the array in PHP
The output array in PHP is The following main methods:
1. print_r() function
The print_r() function prints an array in a human-readable way. It adds a label in front of each element of the array. For example:
<code class="php">$array = ['foo', 'bar', 'baz']; print_r($array);</code>
Output:
<code>Array ( [0] => foo [1] => bar [2] => baz )</code>
2. var_dump() function
var_dump() function prints an array in a more detailed way. It displays the type and value of each element in the array. For example:
<code class="php">$array = ['foo', 'bar', 'baz']; var_dump($array);</code>
Output:
<code>array(3) { [0]=> string(3) "foo" [1]=> string(3) "bar" [2]=> string(3) "baz" }</code>
3. echo() function
echo() function can output any value, including arrays. However, it does not output the array in a formatted manner. For example:
<code class="php">$array = ['foo', 'bar', 'baz']; echo $array;</code>
Output:
<code>Array</code>
4. json_encode() function
json_encode() function converts an array into a JSON string. JSON strings are a lightweight data format that can be easily exchanged with JavaScript and other languages. For example:
<code class="php">$array = ['foo', 'bar', 'baz']; echo json_encode($array);</code>
Output:
<code>["foo","bar","baz"]</code>
The above is the detailed content of In php, what is the method to output an array?. For more information, please follow other related articles on the PHP Chinese website!