在多维数组上的foreach循环

时间:2010-12-14 17:27:39

标签: multidimensional-array

我怎么能像谷歌一样做出漂亮的搜索结果 我无法绕过这么多人。感谢您提供的任何帮助。

Array
(
    [summary] => Array
        (
            [what] => pizza
            [where] => city
        )

    [listings] => Array
        (
            [0] => Array
                (
                    [parent] => 
                    [contents] => Array
                        (
                            [Video] => Array
                                (
                                    [avail] => 
                                )
                        )

                    [id] => 1114638
                    [name] => Sexy house
                    [address] => Array
                        (
                            [street] => 3 King St E
                            [city] => loversLane
                            [prov] => AB
                            [pcode] => L8N1A1
                        )

                    [geoCoded] => Array
                        (
                            [latitude] => 43.256373
                            [longitude] => -79.868167
                        )

                )

        )

)

this works good at printing:

        function recursivePrint($elem) {
   foreach ($elem as $key => $value) {
      if (is_array($value))
         $this->recursivePrint($value);
      else
         print $value.'<br>';
   }
}

但我希望能够在结果上放置链接等。将地理编码作为变量,以便我可以使用地图。与尽可能少的线条一样多的控制。

1 个答案:

答案 0 :(得分:0)

您选择围绕每个HTML的HTML取决于您,但这应该使您明白如何在PHP中使用数组:

echo 'Results for '.$elem['summary']['what'].' '.$elem['summary']['where'].'<br />';

foreach($elem['listings'] as $listing)
{
 echo $listing['name'].'<br />';
 echo $listing['address']['street'].'<br />';
 echo $listing['address']['city'].'<br />';
 echo '<a href="http://maps.google.com/maps?hl=en&tab=wl&q='.$listing['geoCoded']['latitude'].','.$listing['geoCoded']['longitude'].'" title="View location on a map">'.$listing['address']['pcode'].'</a>';
 echo '<br /><br />';
}