0

我从带有两个连接的 MySQL 查询中得到以下结果。

Array ( 
[0] => Array ( [place_id] => 1 [place] => Berlin [lat] => 52.519 [lon] => 13.406 [id] => 1 [pname] => Firstschool [typ] => 0 [s_id] => 32 [fac] => history) 

[1] => Array ( [place_id] => 1 [place] => Berlin [lat] => 52.519 [lon] => 13.406 [id] => 1 [pname] => Secondschool [typ] => 0 [s_id] => 33 [fac] => math)

[2] => Array ( [place_id] => 1 [place] => Berlin [lat] => 52.519 [lon] => 13.406 [id] => 1 [pname] => Secondschool [typ] => 0 [s_id] => 33 [fac] => english)
)

数据在某些时候是多余的,我需要这样:

Array ( 
  [Berlin] => Array ( [lat] => 52.519 
                      [lon] => 13.406  
                      [schools] => Array([0]=> Firstschool [1]=>Secondschool)
  )

  [OtherCity] => Array ( ... )
)

首先,这是可以的还是存在更好的解决方案?=)其次..如何将其拆分为所需的结果。

我尝试了类似以下代码片段的方法,但它没有按预期工作。

foreach($viewmodel as $item) { 
   $data[$item['place']][] = $item['pname'];
}

结果是:

Array ( [Berlin] => Array ( [0] => Firstschool [1] => Firstschool [2] => Firstschool ))

没那么有用。;)

我希望它可以理解我的需要。也许有人对如何解决这个问题有一个好主意。

谢谢你的时间。

4

4 回答 4

1

我认为您走在正确的道路上,只需要填写更多细节:

$cities = Array (
     Array ( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Firstschool', 'typ' => 0, 's_id' => 32, 'fac' => 'history'),
     Array ( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Secondschool', 'typ' => 0, 's_id' => 33, 'fac' => 'math'),
     Array ( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Secondschool', 'typ' => 0, 's_id' => 33, 'fac' => 'english'),
);

// gather the transformed array in a new array 
$out = array();
foreach ($cities as $city) {
    // the first time we see the place
    if (!isset($out[$city['place']])) {
        // copy over what you want to keep
        $out[$city['place']] = array(
            'lat' => $city['lat'],
            'lon' => $city['lon'],
            'schools' => array($city['pname']),
        );
    } // only add $city['pname'] if we don't have it already
    elseif (!in_array($city['pname'], $out[$city['place']]['schools'])) {
        // we already seen this place, just add to the schools
        $out[$city['place']]['schools'][] = $city['pname'];
    }
}

对于收集学院的问题,使用学校名称作为顶级数组的“学校”键中的数组键,像这样填充它们:(仍然跳过重复项):

foreach ($a as $city) {
    if (!isset($out[$city['place']])) {
        $out[$city['place']] = array(
            'lat' => $city['lat'],
            'lon' => $city['lon'],
            'schools' => array($city['pname'] => array($city['fac'])),
        );
    } else {
        // for convenience and readability, introducing some variables
        $schools = &$out[$city['place']]['schools'];
        $pname = $city['pname'];
        $fac = $city['fac'];

        // if we didn't see this school yet, add it with it's faculty
        if (!isset($schools[$pname])) {
            $schools[$pname] = array($fac);
        } // if we did see this school before but the faculty is new, add it under the school's key
        else if (!in_array($fac, $schools[$pname])) { 
            $schools[$pname][] = $fac;
        }
    }
}
于 2013-02-17T19:50:20.887 回答
0

你是对的,你必须以某种方式遍历数组。从我看到的数组中,假设所有学校的所有经纬度都相同,覆盖不会造成伤害,否则需要额外的逻辑

foreach($viewmodel as $item) {
  $data[$item['place']['lat']=$item['lat'];
  $data[$item['place']['long']=$item['lon'];
  $data[$item['place']['schools'][]=$item['pname'];
}
于 2013-02-17T19:49:50.143 回答
0

如果您使用的是 php 5.3+,则可以使用 lambda 函数映射数组

$output = array();

$sort_schools = function($value, $key)
{
    if ( ! is_array($output[$value['place'])
    {
        $output[$value['place'] = array();
    }

    if ( ! isset($output[$value['place']['lat'] && ! isset($output[$value['place']]['lon'])
    {
        $output[$value['place']]['lat'] = $value['lat'];

        $output[$value['place']]['lon'] = $value['lon'];
    }

    $output[$value['place']]['schools'][] = $value['pname'];
};

array_map($sort_schools, $viewmodel);

或者,您可以在 foreach 循环或匿名函数中的 lambda 函数中使用类似的结构。

于 2013-02-17T19:50:31.657 回答
0

以下应产生所描述的预期结果

$arr =  array( 
            array(  'place_id'  => 1,  'place'  => 'Berlin',  'lat'  => 52.519,  'lon'  => 13.406,  'id'  => 1,  'pname'  => 'Firstschool',  'typ'  => 0,  's_id'  => 32,  'fac'  => 'history'),
            array(  'place_id'  => 1,  'place'  => 'Berlin',  'lat'  => 52.519,  'lon'  => 13.406,  'id'  => 1,  'pname'  => 'Secondschool',  'typ'  => 0,  's_id'  => 32,  'fac'  => 'history'),
            array(  'place_id'  => 1,  'place'  => 'Berlin',  'lat'  => 52.519,  'lon'  => 13.406,  'id'  => 1,  'pname'  => 'Secondschool',  'typ'  => 0,  's_id'  => 32,  'fac'  => 'history')
            );

$result = array();

foreach($arr as $item) {
    if (array_key_exists($item['place'], $result)) {
        if (!in_array($item['pname'], $result[$item['place']]['schools'])) {
            array_push($result[$item['place']]['schools'], $item['pname']);
        }
    } else {
        $result[$item['place']]['lat'] = $item['lat'];
        $result[$item['place']]['lon'] = $item['lon'];
        $result[$item['place']]['schools'][] = $item['pname'];
    }
}

print_r($result);

哪个应该输出

 Array (
     [Berlin] => Array
     (
         [lat] => 52.519
         [lon] => 13.406
         [schools] => Array
             (
                 [0] => Firstschool
                 [1] => Secondschool
             )

     )
)
于 2013-02-17T20:05:16.467 回答