dqx76962 2010-08-15 22:28
浏览 69

比较和组合多维数组

I am reading values from a Wordpress database into an array. I am then plotting these to a Google map.

The problem is i have multiple items in the same location, and when plotted to the map i can only see one of them (i presume multiple entries are covering each other.

My output in this test case is:

    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
        )

As you can see there are duplicate geo values for various items.

What i would like to do is combine elements with identical geo location so i can use them the same on the output.

eg:

geo location 1 - value geo location 2 - value + other values for identical geo location.

Does that make sense?

Update:

I cant get it to work. My code is:

    $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'];
    }
}


?>

which is outputting:

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

                )

        )

)

The Etcetera Theatre
The Twenty Minute Policy at The Etcetera Theatre - Show dates - May 10, 2010
The Lion and Unicorn
Happy Everyday! at The Lion and Unicorn - Show dates - May 10, 2010
Theatro Technis
Malowe at Theatro Technis - Show dates - May 7, 2010
The Rosemary Branch
Stay With Me Till Dawn at The Rosemary Branch - Show dates - January 19, 2010
5<5<
Leeds, England, UK
Imicus – Visceral - Image Gallery - October 23, 2009
London, England, UK
Rob Is - Image Gallery - September 22, 2009
Camden People\'s Theatre
Rob Is at Camden People’s Theatre - Show dates - August 10, 2009
Southampton, England, UK
Anniversary - Image Gallery - August 8, 2009
New End Theatre
Instillation at The New End Theatre - Show dates - May 13, 2009
Central School of Speech and Drama, London, England, UK
The Tempest - Image Gallery - May 12, 2009
London Film School, Covent Garden, England UK
Jack and Jessies - Image Gallery - May 10, 2009
5<
Robinson College Garden\'s
As you like it at Robinson College Garden’s - Show dates - August 18, 2008
5<
St John\'s College
As you like it at St John’s College - Show dates - July 7, 2008
Aviemore, Scotland, UK
Steppenwolf - Image Gallery - June 12, 2008
Birmingham, England, UK
Solo Night - Image Gallery - December 15, 2007
Birmingham, England, UK
Caged - Image Gallery - August 8, 2007
Birmingham, England, UK
Friends - Image Gallery - April 4, 2007
Birmingham, England, UK
Get Lost - Image Gallery - May 1, 2006
Birmingham, England, UK
Five Days - Image Gallery - February 1, 2006
Birmingham, England, UK
CopyCat - Image Gallery - September 25, 2005
Leicester, England, UK
Gift - Image Gallery - January 10, 2005

Seem to have some duplicate key values.

  • 写回答

1条回答 默认 最新

  • dqzd92796 2010-08-15 22:33
    关注

    You could restructure your array to store arrays of items at the same geograpic location, using geo as the key:

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

    Giving:

    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
            )
        )
    

    Finally, to iterate over this new array:

    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
             *         )
             */
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100