I have created a 2D Array and trying to access each elements but i am receiving wrong value of each array .I can see only Today as the value
<?php
$blog = array (
0 => array('by' => "Nilay Mehta", 'on' => "Today", 'id' => "Today", 'post' => "Today"),
1 => array('by' => "Nilay Mehta", 'on' => "Today", 'id' => "Today", 'post' => "Today"),
2 => array('by' => "Nilay Mehta", 'on' => "Today", 'id' => "Today", 'post' => "Today"),
3 => array('by' => "Nilay Mehta", 'on' => "Today", 'id' => "Today", 'post' => "Today"),
);
foreach($blog as $blog)
{
echo "<a href=\"#\">".$blog['by']."</a>";
echo "<div class=\"blogtime\"> - ".$blog['on']."</div>";
echo "<div class=\"blogtime\">Blog Post ID - ".$blog['id'];
echo $blog['post'];
}
?>
this as $blog
will overwrite the original $blog
variable and for the first time you can access the value but then it will not work as expected. change the second $blog
to anything else as I did
foreach($blog as $b)
{
echo "<a href=\"#\">".$b['by']."</a>";
echo "<div class=\"blogtime\"> - ".$b['on']."</div>";
echo "<div class=\"blogtime\">Blog Post ID - ".$b['id'];
echo $b['post'];
}
in your case the $blog
will become from
array (
0 => array('by' => "Nilay Mehta", 'on' => "Today", 'id' => "Today", 'post' => "Today"),
1 => array('by' => "Nilay Mehta", 'on' => "Today", 'id' => "Today", 'post' => "Today"),
2 => array('by' => "Nilay Mehta", 'on' => "Today", 'id' => "Today", 'post' => "Today"),
3 => array('by' => "Nilay Mehta", 'on' => "Today", 'id' => "Today", 'post' => "Today"),
);
to
array('by' => "Nilay Mehta", 'on' => "Today", 'id' => "Today", 'post' => "Today");
Try using foreach($blog as $ind_blog)
instead of foreach($blog as $blog)
, in current system $blog
array replaced by the first occurance of the loop with same variable..