通过多维数组循环并获取键的值

时间:2014-06-22 07:12:22

标签: php arrays

如何获得下面多维数组的所有纬度和经度并将其存储在单独的数组中。

Array
(
    [Beauty & Wellness] => Array
        (
            [kategorie_icon_0] => abc
            [kategorie_icon_1] => cde
            [partnerlist] => Array
                (
                    [0] => Array
                        (
                            [id] => 39552
                            [latitude] => 49.773109
                            [longitude] => 8.641460
                        )   
                     [1] => Array
                        (
                            [id] => 39550
                            [latitude] => 49.871452
                            [longitude] => 8.643979
                        )
                )       
        )
    [fashion] => Array
        (
            [kategorie_icon_0] => abc
            [kategorie_icon_1] => cde
            [partnerlist] => Array
                (
                    [0] => Array
                        (
                            [id] => 39552
                            [latitude] => 49.773109
                            [longitude] => 8.641460
                        )   
                     [1] => Array
                        (
                            [id] => 39550
                            [latitude] => 49.871452
                            [longitude] => 8.643979
                        )
                )       
        )
)

2 个答案:

答案 0 :(得分:0)

//Initiate new array to store coords
$latlongs = array();

//Loop through your array
foreach($yourArray as $k=>$v){
      // Loop through the partnerlist to extract lat/lon
      // Append to your coord array, and preserve the industry key
      // So that you know which lat/lons came from where
      foreach($v['partnerlist'] as $a){
         $latlongs[$k][] = array("latitude"=>$a['latitude'],"longitude"=>$a['longitude'];
      }
}

这将保留密钥,以便您可以按原始类别将所有坐标组合在一起。

如果你不关心这个并且纯粹只想要lat / long,那么就干脆...

foreach($yourArray as $v){
      foreach($v['partnerlist'] as $a){
         $latlongs[] = array("latitude"=>$a['latitude'],"longitude"=>$a['longitude'];
      }
}

答案 1 :(得分:0)

$coords = array();

foreach ($master_array as $array)
{
    foreach ($array['partnerlsit'] as $partner)
    {
        $coords []= array('lat' => $partner['latitude'], 'lng' => $partner['longitude']);

    } 
}