echo an array in php code example

Example 1: php echo arry

echo json_encode($anArray);

Example 2: php print array

// raw array output
print_r($arr);

// the above well-formatted
echo '<pre>'; print_r($array); echo '</pre>';

// more details like datatype and length
var_dump($arr);

// output that PHP understands
var_export($arr);

// by foreach loop
foreach($arr as $key=>$value)
  echo $key, '=>', $value;	// $value must be convertible to string

Example 3: php echo array

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
?>

Example 4: php echo array

$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);

Example 5: php echo array

foreach($results as $result) {
	echo $result . '<br>';
}

Example 6: php echo array

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

Tags:

Php Example