我从带有两个连接的 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 ))
没那么有用。;)
我希望它可以理解我的需要。也许有人对如何解决这个问题有一个好主意。
谢谢你的时间。