在Php中解析嵌套数组

时间:2015-08-08 17:52:48

标签: php arrays iterator nested-loops

如何遍历PHP数组 这是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];


}

它的假设输出是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>
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>

修改

原因是一个错字  echo $value[$row]['place'];而不是echo $value[$row][place];解决了问题

2 个答案:

答案 0 :(得分:1)

使用foreach浏览php数组

foreach ($array as $row) {
    echo $row['place'].' '.$row['Distance'].' '.$row['Lat'].' '.$row['Lng'];
}

答案 1 :(得分:1)

你可以这样试试,

<?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'] . " ";
}

?>