我正在将Wordpress数据库中的值读入数组。然后我将这些绘制到谷歌地图上。
问题是我在同一个位置有多个项目,当绘制到地图时我只能看到其中一个(我假设多个条目相互覆盖。
此测试用例中的输出是:
Array
(
[0] => Array
(
[geo] => 51.540697,-0.144197
[type] => show
[timestamp] => May 10, 2010
[title] => The Twenty Minute Policy at The Etcetera Theatre
[permalink] => http://localhost.davidswain/show/the-twenty-minute-policy-the-etcetera-theatre
)
[1] => Array
(
[geo] => 51.547879,-0.138853
[type] => show
[timestamp] => May 10, 2010
[title] => Happy Everyday! at The Lion and Unicorn
[permalink] => http://localhost.davidswain/show/happy-everyday-lion-and-unicorn
)
[3] => Array
(
[geo] => 51.537475,-0.086871
[type] => show
[timestamp] => January 19, 2010
[title] => Stay With Me Till Dawn at The Rosemary Branch
[permalink] => http://localhost.davidswain/show/stay-with-me-till-dawn-at-the-rosemary-branch
)
[4] => Array
(
[geo] => 51.537475,-0.086871
[type] => show
[timestamp] => January 15, 2010
[title] => The Other Roof at The Rosemary Branch
[permalink] => http://localhost.davidswain/show/the-other-roof-at-the-rosemary-branch
)
[5] => Array
(
[geo] => 51.547879,-0.138853
[type] => show
[timestamp] => November 24, 2009
[title] => Supernatural at The Lion & Unicorn Theatre
[permalink] => http://localhost.davidswain/show/supernatural-at-the-lion-unicorn-theatre
)
[6] => Array
(
[geo] => 53.799639,-1.549122
[type] => images
[timestamp] => October 23, 2009
[title] => Imicus – Visceral
[permalink] => http://localhost.davidswain/images/imicus-visceral
)
正如您所看到的,各种项目都存在重复的地理位置值。
我想要做的是将具有相同地理位置的元素组合在一起,这样我就可以在输出中使用它们。
例如:
地理位置1 - 价值 地理位置2 - 相同地理位置的值+其他值。
这有意义吗?
我无法让它发挥作用。我的代码是:
$args = array(
'post_type' => array('post', 'page', 'role', 'images', 'showreel', 'training', 'skill', 'show' ),
'meta_key' => 'geo_value',
'posts_per_page' => -1
);
$wherePosts = new WP_Query($args);
while ($wherePosts->have_posts()) : $wherePosts->the_post();
if (get_post_type() == "images") {
$type = "Image Gallery";
} elseif (get_post_type() == "show") {
$type = "Show dates";
}
$venue = addslashes(get_post_meta($post->ID, 'venue_value', true));
$area = addslashes(get_post_meta($post->ID, 'area_value', true));
if (!empty($venue)) {
$location = $venue;
} elseif (!empty($area)) {
$location = $area;
}
$current = get_the_ID();
$thePosts[] = array(
geo => get_post_meta($post->ID, 'geo_value', true),
str => '<div class="map-overlay"><b>' . $location . '</b><p><a href="' . get_permalink() . '">' . get_the_title() . '</a> - ' . $type .' - ' . get_the_date() . '</p></div>'
);
endwhile;
wp_reset_query();
//sort array
//print_r($thePosts);
$locationArray = Array();
foreach ($thePosts as $value) {
if ( isset($locationArray[$value['geo']] )) {
$locationArray[] = $value;
} else {
$locationArray[$value['geo']] = Array($value);
}
}
?>
<pre>
<?php
print_r($locationArray);
//ksort($geoArray);
print_r($geoArray);
?>
</pre>
<?php
foreach($locationArray as $geo => $items){
foreach($items as $item){
echo $item['str'];
}
}
?>
输出的:
Array
(
[51.540697,-0.144197] => Array
(
[0] => Array
(
[geo] => 51.540697,-0.144197
[str] =>
The Etcetera Theatre
The Twenty Minute Policy at The Etcetera Theatre - Show dates - May 10, 2010
)
)
[51.547879,-0.138853] => Array
(
[0] => Array
(
[geo] => 51.547879,-0.138853
[str] =>
The Lion and Unicorn
Happy Everyday! at The Lion and Unicorn - Show dates - May 10, 2010
)
)
[51.535262,-0.135425] => Array
(
[0] => Array
(
[geo] => 51.535262,-0.135425
[str] =>
Theatro Technis
Malowe at Theatro Technis - Show dates - May 7, 2010
)
)
[51.537475,-0.086871] => Array
(
[0] => Array
(
[geo] => 51.537475,-0.086871
[str] =>
The Rosemary Branch
Stay With Me Till Dawn at The Rosemary Branch - Show dates - January 19, 2010
)
)
[0] => Array
(
[geo] => 51.537475,-0.086871
[str] =>
The Rosemary Branch
The Other Roof at The Rosemary Branch - Show dates - January 15, 2010
)
[1] => Array
(
[geo] => 51.547879,-0.138853
[str] =>
The Lion and Unicorn
Supernatural at The Lion & Unicorn Theatre - Show dates - November 24, 2009
)
[53.799639,-1.549122] => Array
(
[0] => Array
(
[geo] => 53.799639,-1.549122
[str] =>
Leeds, England, UK
Imicus – Visceral - Image Gallery - October 23, 2009
)
)
[51.551706,-0.158825] => Array
(
[0] => Array
(
[geo] => 51.551706,-0.158825
[str] =>
London, England, UK
Rob Is - Image Gallery - September 22, 2009
)
)
[51.527488,-0.138667] => Array
(
[0] => Array
(
[geo] => 51.527488,-0.138667
[str] =>
Camden People\'s Theatre
Rob Is at Camden People’s Theatre - Show dates - August 10, 2009
)
)
[50.904966,-1.403234] => Array
(
[0] => Array
(
[geo] => 50.904966,-1.403234
[str] =>
Southampton, England, UK
Anniversary - Image Gallery - August 8, 2009
)
)
[51.558643,-0.176306] => Array
(
[0] => Array
(
[geo] => 51.558643,-0.176306
[str] =>
New End Theatre
Instillation at The New End Theatre - Show dates - May 13, 2009
)
)
[51.544334,-0.174204] => Array
(
[0] => Array
(
[geo] => 51.544334,-0.174204
[str] =>
Central School of Speech and Drama, London, England, UK
The Tempest - Image Gallery - May 12, 2009
)
)
[51.513548,-0.125454 ] => Array
(
[0] => Array
(
[geo] => 51.513548,-0.125454
[str] =>
London Film School, Covent Garden, England UK
Jack and Jessies - Image Gallery - May 10, 2009
)
)
[2] => Array
(
[geo] => 51.544334,-0.174204
[str] =>
Central School of Speech and Drama
The Tempest at Central School of Speech and Drama - Show dates - January 16, 2009
)
[52.20479,0.105361] => Array
(
[0] => Array
(
[geo] => 52.20479,0.105361
[str] =>
Robinson College Garden\'s
As you like it at Robinson College Garden’s - Show dates - August 18, 2008
)
)
[3] => Array
(
[geo] => 52.20479,0.105361
[str] =>
Robinson College Garden\'s
The Comedy of Errors at Robinson College Garden’s - Show dates - July 28, 2008
)
[52.208748,0.115528] => Array
(
[0] => Array
(
[geo] => 52.208748,0.115528
[str] =>
St John\'s College
As you like it at St John’s College - Show dates - July 7, 2008
)
)
[57.186479,-3.828585 ] => Array
(
[0] => Array
(
[geo] => 57.186479,-3.828585
[str] =>
Aviemore, Scotland, UK
Steppenwolf - Image Gallery - June 12, 2008
)
)
[52.504053,-1.881228] => Array
(
[0] => Array
(
[geo] => 52.504053,-1.881228
[str] =>
Birmingham, England, UK
Solo Night - Image Gallery - December 15, 2007
)
)
[52.482961,-1.893592] => Array
(
[0] => Array
(
[geo] => 52.482961,-1.893592
[str] =>
Birmingham, England, UK
Caged - Image Gallery - August 8, 2007
)
)
[52.510958,-1.865101] => Array
(
[0] => Array
(
[geo] => 52.510958,-1.865101
[str] =>
Birmingham, England, UK
Friends - Image Gallery - April 4, 2007
)
)
[52.476211,-1.89793] => Array
(
[0] => Array
(
[geo] => 52.476211,-1.89793
[str] =>
Birmingham, England, UK
Shopping and Fucking - Image Gallery - March 11, 2007
)
)
[52.477448,-1.915939] => Array
(
[0] => Array
(
[geo] => 52.477448,-1.915939
[str] =>
Crescent Theatre
Shopping and Fucking at the Crescent Theatre - Show dates - March 5, 2007
)
)
[53.383055,-1.464795] => Array
(
[0] => Array
(
[geo] => 53.383055,-1.464795
[str] =>
Sheffield, England, UK
Get Lost - Image Gallery - May 1, 2006
)
)
[52.451825,-1.89236] => Array
(
[0] => Array
(
[geo] => 52.451825,-1.89236
[str] =>
Birmingham, England, UK
Five Days - Image Gallery - February 1, 2006
)
)
[52.473299,-1.89029] => Array
(
[0] => Array
(
[geo] => 52.473299,-1.89029
[str] =>
Birmingham, England, UK
CopyCat - Image Gallery - September 25, 2005
)
)
[52.63477,-1.129519] => Array
(
[0] => Array
(
[geo] => 52.63477,-1.129519
[str] =>
Leicester, England, UK
Gift - Image Gallery - January 10, 2005
)
)
)
Etcetera剧院
The Etcetera Theatre的二十分钟政策 - 显示日期 - 2010年5月10日
狮子和独角兽
每天快乐!在狮子和独角兽 - 显示日期 - 2010年5月10日
Theatro Technis
Theatro Technis的Malowe - 显示日期 - 2010年5月7日
迷迭香分支
和我呆在一起到黎明迷迭香分店 - 显示日期 - 2010年1月19日
第5版,第5版,
利兹,英格兰,英国
Imicus - Visceral - 图片库 - 2009年10月23日
伦敦,英国,英国
Rob Is - 图片库 - 2009年9月22日
卡姆登人民剧院 Rob在Camden人民剧院 - 显示日期 - 2009年8月10日
南安普敦,英格兰,英国
周年纪念 - 图片库 - 2009年8月8日
新端剧院
新端剧院的灌输 - 显示日期 - 2009年5月13日
中央英语演讲与戏剧学院,英国伦敦,英国 暴风雨 - 图片库 - 2009年5月12日
伦敦电影学院,英国科文特花园 杰克和杰西 - 图片库 - 2009年5月10日
5℃;
罗宾逊学院花园的 如你所愿,在罗宾逊学院花园 - 显示日期 - 2008年8月18日
5℃;
圣约翰学院 如你所愿,在圣约翰学院 - 展览日期 - 2008年7月7日
阿维莫尔,苏格兰,英国
荒原狼 - 图片库 - 2008年6月12日
伯明翰,英格兰,英国
独奏之夜 - 图片库 - 2007年12月15日
伯明翰,英格兰,英国
笼中 - 图像画廊 - 2007年8月8日
伯明翰,英格兰,英国
朋友 - 图片库 - 2007年4月4日
伯明翰,英格兰,英国
迷失 - 图片库 - 2006年5月1日
伯明翰,英格兰,英国
五天 - 图片库 - 2006年2月1日
伯明翰,英格兰,英国
CopyCat - 图像库 - 2005年9月25日
莱斯特,英格兰,英国
礼品 - 图片库 - 2005年1月10日
似乎有一些重复的键值。
答案 0 :(得分:4)
您可以使用geo
作为关键字重组数组以在同一地理位置存储项目数组:
$locationArray = Array()
foreach($geoArray as $value){
if(isset($locationArray[$value['geo']]){
$locationArray[] = $value;
}
else{
$locationArray[$value['geo']] = Array($value);
}
}
print_r($locationArray);
,并提供:
Array(
['51.540697,-0.144197'] => Array(
[0] = Array(
[geo] => 51.540697,-0.144197
[type] => show
[timestamp] => May 10, 2010
[title] => The Twenty Minute Policy at The Etcetera Theatre
[permalink] => http://localhost.davidswain/show/the-twenty-minute-policy-the-etcetera-theatre
)
)
['51.547879,-0.138853'] = Array(
[0] => Array(
[geo] => 51.547879,-0.138853
[type] => show
[timestamp] => May 10, 2010
[title] => Happy Everyday! at The Lion and Unicorn
[permalink] => http://localhost.davidswain/show/happy-everyday-lion-and-unicorn
)
[1] => Array(
[geo] => 51.547879,-0.138853
[type] => show
[timestamp] => November 24, 2009
[title] => Supernatural at The Lion & Unicorn Theatre
[permalink] => http://localhost.davidswain/show/supernatural-at-the-lion-unicorn-theatre
)
)
['51.537475,-0.086871'] => Array(
[0] = Array(
[geo] => 51.537475,-0.086871
[type] => show
[timestamp] => January 19, 2010
[title] => Stay With Me Till Dawn at The Rosemary Branch
[permalink] => http://localhost.davidswain/show/stay-with-me-till-dawn-at-the-rosemary-branch
)
[1] => Array(
[geo] => 51.537475,-0.086871
[type] => show
[timestamp] => January 15, 2010
[title] => The Other Roof at The Rosemary Branch
[permalink] => http://localhost.davidswain/show/the-other-roof-at-the-rosemary-branch
)
)
['53.799639,-1.549122'] => Array(
[0] = Array(
[geo] => 53.799639,-1.549122
[type] => images
[timestamp] => October 23, 2009
[title] => Imicus – Visceral
[permalink] => http://localhost.davidswain/images/imicus-visceral
)
)
最后,迭代这个新数组:
foreach($locationArray as $geo => $items){
foreach($items as $item){
/*
* Do your stuff with the $item:
*
* $item = Array(
* [geo] => 53.799639,-1.549122
* [type] => images
* [timestamp] => October 23, 2009
* [title] => Imicus – Visceral
* [permalink] => http://localhost.davidswain/images/imicus-visceral
* )
*/
}
}