Thursday, 6 October 2011

Print array values in php


To print array values following method are used. For your need use correct method

This code will display the text "apple"

$fruit[0] = "apple";
$fruit[1] = "banana";
$fruit[2] = "orange";
echo $fruit[0]; 


This display all elements in the array. but u need to count a array

for ($i=0;$i<=2;$i++){
    echo $fruit[$i]; 
}

Using count array

$count = count($fruit);
for ($i=0;$i<=$count;$i++){
    echo $fruit[$i];
}

This is a best way to display all the element in the array

foreach ($fruit as $value){
    echo $value;   
}

0 comments:

Post a Comment