Posted under » PHP on 13 April 2009
For Looping in associative array in JSON
Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to perform this.
The do...while Statement
<¿php
$i=0;
do
{
$i++;
echo "The number is " . $i . "
";
}
while ($i<5);
?>
The for statement is the most advanced of the loops in PHP.
<¿php
for ($i=1; $i<=5; $i++)
{
echo "Hello World!
";
}
?>
The foreach statement is used to loop through arrays.
<¿php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "
";
}
?>
Lets say you want to add commas to the array.
<¿php
$arr=array("one", "two", "three");
$i=0;
foreach ($arr as $value)
{
$i++;
if ($i > 1) { $numb .= ", "; }
$numb .= $value;
}
echo $numb
?>
You can sort arrays.