How to Echo or Print an Array in PHP

How can I echo or print an array in PHP?

This will do

foreach($results['data'] as $result) {
    echo $result['type'], '<br>';
}

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().

Cannot echo the values in an array in Php

Because
echo — Output one or more STRINGS

http://php.net/manual/en/function.echo.php

Try using var_dump for array
http://php.net/manual/en/function.var-dump.php

or print_r
http://php.net/manual/en/function.print-r.php

how to echo print array in mysql php

while($row = $language_name->fetch()) {
$allRows[] = $row;
}

then in php

foreach($allRows as $value){
echo $value['language_id'];
}

How to print or echo the array index of in PHP

If you're talking about an associative array, you need to fetch the index directly:

Example:

$array = array ('test' => 'value1', 'test2' => 'value2');   
echo $array['test']; // value1

You can do a print_r($array) to see the array structure nicely formatted:

<pre>
<?php print_r($array);?>
</pre>

What you're doing instead is fetch a value by its numerical index, like in

$array = array('test','test2','test3');
echo $array[0]; // test

On a further note, you can check beforehand if a key exists using array_key_exists():

var_dump(array_key_exists('test2',$array));  // (bool) TRUE

How to print output in array in php?

Instead of printing the values -

echo $str . "\n";

Save it to an array

$ordersNewArray[] = $str;

So it would be something like -

<?php
$rule =
[
"c" => "d",
"a" => "o",
"t" => "g",
"h" => "a",
"1" => "@",
"e" => "n",
"n" => "t"
];
$orders = ['cat1hen','cathen','hencat'];

//Create new Array
$ordersNewArray = [];

foreach($orders as $order){
$arr = str_split($order);

$str ="";
foreach($arr as $key){
$str .= $rule[$key];
}
// Save to new Array
$ordersNewArray[] = $str;
}

//IF you want to verify, notice this is **after** the loop is done
print_r($ordersNewArray);

php foreach echo prints “Array” as value

So simple that means $row is an array not an variable. You need to do print_r($row); instead of echo $row;

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;


Related Topics



Leave a reply



Submit