Output (Echo/Print) Everything from a PHP Array

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



Leave a reply



Submit