Output (echo/print) everything from a PHP Array
If you want to format the output on your own, simply add another loop (foreach) to iterate through the contents of the current row:
while ($row = mysql_fetch_array($result)) {
foreach ($row as $columnName => $columnData) {
echo 'Column name: ' . $columnName . ' Column data: ' . $columnData . '<br />';
}
}
Or if you don't care about the formatting, use the print_r function recommended in the previous answers.
while ($row = mysql_fetch_array($result)) {
echo '<pre>';
print_r ($row);
echo '</pre>';
}
print_r() prints only the keys and values of the array, opposed to var_dump() whichs also prints the types of the data in the array, i.e. String, int, double, and so on. If you do care about the data types - use var_dump() over print_r().
How can I echo or print an array in PHP?
This will do
foreach($results['data'] as $result) {
echo $result['type'], '<br>';
}
how to echo print_r() array output
Loop the array like shown below. The key is the email, then use implode()
on the value
foreach ($array as $key => $value) {
echo "key: " , $key , PHP_EOL;
echo "value: " , implode(' ',$value) , PHP_EOL , PHP_EOL;
}
Output:-
key: one@gmail.com
value: 70,80 90,100
key: two@gmail.com
value: 10
Demo at:
https://3v4l.org/gXJcP or https://3v4l.org/rN9LV
How to echo an array in PHP?
To get the exact output you requested, you could encode the array as JSON, e.g.
echo json_encode($stocked);
How can I print all the values of an array?
So many ways to do it...
foreach ($array as $item) {
echo $item;
}
echo join(', ', $array);
array_walk($array, create_function('$a', 'echo $a;'));
Display array values in PHP
There is foreach loop in php. You have to traverse the array.
foreach($array as $key => $value)
{
echo $key." has the value". $value;
}
If you simply want to add commas between values, consider using implode
$string=implode(",",$array);
echo $string;
Is there any way to get the output of PHP array like it's defined
Seems like you are needing it for reading purposes.. for that make use of json_encode()
;
<?php
$array = ['a','b','c','d'];
echo json_encode($array); //"prints" ["a","b","c","d"]
Related Topics
Get Request from PHP Using File_Get_Contents with Parameters
PHP Domdocument - Get HTML Source of Body
How to Get/Set Session_Id() or Should It Be Generated Automatically
PHP Get Height and Width in PDF File Proprieties
How to Parse This Table and Extract Data from It
Symfony & Guard: "The Security Token Was Removed Due to an Accountstatusexception"
Html2Pdf in PHP - Convert Utilities & Scripts - Examples & Demos
Phpdoc for Variable-Length Arrays of Arguments
How to Access MySQLi Connection in Another Class on Another Page
Check If Username Exists in MySQL Table via PHP
Nl2Br() Equivalent in JavaScript
Laravel Expected Response Code 250 But Got Code "530"
How to Allow a User to Download a File Which Is Stored Outside of the Webroot
Replace the Last Comma with an & Sign
Implementing Oauth2 Login, Fatal Error: Class 'Google_Service' Not Found
Differencebetween Crc32 and Crc32B
How to Get the Tag "<Yweather:Condition>" from Yahoo Weather Rss in PHP