How to output or print the array results in php

array

To print an array result using var_dump()

<?php
$a = array(1, 2, array(“a”, “b”, “c”));
var_dump($a);
?>

Note: var_dump() is used to dumb the information about the variable

output

array
  0 => int 1
  1 => int 2
  2 => 
    array
      0 => string 'a' (length=1)
      1 => string 'b' (length=1)
      2 => string 'c' (length=1)

To print an array result using print_r()

<?php
$a =array(“a”, “b”, “c”);
print_r($a);
?>

Note: print_r() is used to print the human readable information about an variable

output

Array ( [0] => a [1] => b [2] => c )

 

To print an array result using for loop

<?php
$chocolate = array(“FiveStar”, “Milkybar”, “DiaryMilk”);
$arrlength = count($chocolate);

for($x = 0; $x <  $arrlength; $x++) {
echo $chocolate[$x];
echo “<br>”;
}
?>

output

FiveStar
Milkybar
DairyMilk

To print an array result using foreach loop

<?php
$chocolate = array(“FiveStar”, “Milkybar”, “DiaryMilk”);
foreach ($chocolate as $value) {
echo “$value <br>”;
}
?>

output

FiveStar
Milkybar
DairyMilk

Leave a Comment