开发者

foreach loop on multidimensional array

开发者 https://www.devze.com 2023-01-31 02:32 出处:网络
how could I make a nice search result like google I cannot wrap my head around this many. thanks for any help you can give.

how could I make a nice search result like google I cannot wrap my head around this many. thanks for any help you can give.

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 a开发者_如何学运维t printing:

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

But I want to be able to place links over the results etc. have the geocode as variables so I can use maps. Just as much control with as little lines as possible.


The HTML you choose to wrap round each one is up to you, but this should make it obvious how you use arrays in 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 />';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消