-3

How to iterate through PHP array This is the output of var_export

   array (
      0 => 
      array (
        'place' => 'GreenPepper',
        'Distance' => '0.487',
        'Lat' => '8.52699',
        'Lng' => '76.92419100000006',
      ),
      1 => 
      array (
        'place' => 'BAKE \'N\' COOL',
        'Distance' => '0.513',
        'Lat' => '8.527908',
        'Lng' => '76.92643599999997',
      ),
    )

PHP

for ($row = 0; $row < sizeof($obj); $row++) {

 echo $value[$row][place];


}

It the suppossed output was GreenPepper0.487 but instead it was getting this

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Use of undefined constant place - assumed 'place' in C:\wamp\www\SortJson.php on line <i>28</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>246592</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\SortJson.php' bgcolor='#eeeeec'>..\SortJson.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Use of undefined constant Distance - assumed 'Distance' in C:\wamp\www\SortJson.php on line <i>28</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>246592</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\SortJson.php' bgcolor='#eeeeec'>..\SortJson.php<b>:</b>0</td></tr>
</table></font>
GreenPepper0.487<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Use of undefined constant place - assumed 'place' in C:\wamp\www\SortJson.php on line <i>28</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>246592</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\SortJson.php' bgcolor='#eeeeec'>..\SortJson.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Use of undefined constant Distance - assumed 'Distance' in C:\wamp\www\SortJson.php on line <i>28</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>246592</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\SortJson.php' bgcolor='#eeeeec'>..\SortJson.php<b>:</b>0</td></tr>
</table></font>

EDIT

The cause was a typo echo $value[$row]['place']; instead of this echo $value[$row][place]; solved the issue

codefreaK
  • 3,584
  • 5
  • 34
  • 65

2 Answers2

1

Use a foreach to go through your php array

foreach ($array as $row) {
    echo $row['place'].' '.$row['Distance'].' '.$row['Lat'].' '.$row['Lng'];
}
kidA
  • 1,379
  • 1
  • 9
  • 19
1

You can try it like this,

<?php

$myArray = array (
      0 => 
      array (
        'place' => 'GreenPepper',
        'Distance' => '0.487',
        'Lat' => '8.52699',
        'Lng' => '76.92419100000006',
      ),
      1 => 
      array (
        'place' => 'BAKE \'N\' COOL',
        'Distance' => '0.513',
        'Lat' => '8.527908',
        'Lng' => '76.92643599999997',
      ),
    );

foreach($myArray as $value){
    echo $value['place'] . " ";
    echo $value['Distance'] . " ";
    echo $value['Lat'] . " ";
    echo $value['Lng'] . " ";
}

?>
Chaya Sandamali
  • 657
  • 9
  • 22