首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多维数组的比较与组合

多维数组的比较与组合
EN

Stack Overflow用户
提问于 2010-08-15 22:28:41
回答 1查看 187关注 0票数 0

我从Wordpress数据库中读取值到数组中。然后,我将这些绘制成谷歌地图。

问题是,我在同一个位置有多个条目,当绘制到地图上时,我只能看到其中的一个(我假设多个条目相互覆盖。

我在这个测试用例中的输出是:

代码语言:javascript
复制
    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
        )

如您所见,各种项都有重复的geo值。

我想要做的是将具有相同地理位置的元素组合在一起,这样我就可以在输出中使用它们。

例:

地理位置1-值地理位置2-值+相同地理位置的其他值。

这有意义吗?

更新:

我不能让它开始工作。我的代码是:

代码语言:javascript
复制
    $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'];
    }
}


?>

正在输出:

代码语言:javascript
复制
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剧院

Etcetera剧院的20分钟政策-放映日期-2010年5月10日

狮子和独角兽

每一天快乐!在狮子和独角兽的展览日期-2010年5月10日

戏院技术

马洛威在技术剧院-展览日期-2010年5月7日

迷迭香分会

呆在我身边,直到黎明时分迷迭香分店-展示日期-2010年1月19日

5<5<

英国利兹

伊米克斯-内脏-图片画廊-2009年10月23日

伦敦,英格兰,英国

Rob Is -图片画廊-2009年9月22日

卡姆登人民剧院

罗伯在卡姆登人民剧院演出日期-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日

英国伯明翰

笼屋-影像廊-二零零七年八月八日

英国伯明翰

“朋友”-图片廊-二零零七年四月四日

英国伯明翰

迷失-图片库-2006年5月1日

英国伯明翰

五天-图片廊-二零零六年二月一日

英国伯明翰

CopyCat -影像廊-二零零五年九月二十五日

英国莱斯特,英国

礼物-图片画廊-2005年1月10日

似乎有一些重复的键值。

EN

回答 1

Stack Overflow用户

发布于 2010-08-15 22:33:52

您可以重构数组以将项数组存储在相同的地理位置,使用geo作为键:

代码语言:javascript
复制
$locationArray = Array()

foreach($geoArray as $value){

    if(isset($locationArray[$value['geo']]){
        $locationArray[] = $value;
    }
    else{
        $locationArray[$value['geo']] = Array($value);
    }
}

print_r($locationArray);

给予:

代码语言:javascript
复制
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 &#038; 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 &#8211; Visceral
            [permalink] => http://localhost.davidswain/images/imicus-visceral
        )
    )

最后,迭代这个新数组:

代码语言:javascript
复制
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 &#8211; Visceral
         *             [permalink] => http://localhost.davidswain/images/imicus-visceral
         *         )
         */
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3489532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档