递归多维数组清理

时间:2015-09-24 14:08:22

标签: php arrays coordinates

我有一个存储时髦的zip坐标数组。这是一个例子:

[ 
[ 
    [ 
        [ -86.100244, 31.240460 ], [ -86.093814, 31.240430 ], [ -86.092085, 31.241552 ], [ -86.090261, 31.247551 ], [ -86.090022, 31.249796 ], [ -86.090372, 31.253548 ], [ -86.091308, 31.257124 ], [ -86.090297, 31.262423 ], [ -86.089312, 31.263913 ], [ -86.089339, 31.266277 ], [ -86.088645, 31.268668 ], [ -86.082300, 31.264919 ], [ -86.080802, 31.262602 ], [ -86.080393, 31.254969 ], [ -86.078874, 31.252814 ], [ -86.071737, 31.249901 ], [ -86.063719, 31.247637 ], [ -86.059080, 31.247375 ], [ -86.054421, 31.248294 ], [ -86.041221, 31.249039 ], [ -86.038034, 31.249027 ], [ -86.038196, 31.248774 ], [ -86.039120, 31.247187 ], [ -86.041291, 31.243459 ], [ -86.043650, 31.240295 ], [ -86.044764, 31.238032 ], [ -86.051462, 31.237612 ], [ -86.058916, 31.237665 ], [ -86.058719, 31.226191 ], [ -86.058536, 31.222940 ], [ -86.084549, 31.222445 ], [ -86.084921, 31.223183 ], [ -86.084812, 31.227491 ], [ -86.083678, 31.229595 ], [ -86.088507, 31.229438 ], [ -86.093104, 31.229694 ], [ -86.094768, 31.230409 ], [ -86.097699, 31.233113 ], [ -86.098889, 31.233219 ], [ -86.099265, 31.237281 ], [ -86.100244, 31.240460 ] 
    ] 
], 
[ 
    [ 
        [ -86.255008, 31.103419 ], [ -86.253618, 31.109155 ], [ -86.251058, 31.112943 ], [ -86.247938, 31.112895 ], [ -86.246462, 31.113440 ], [ -86.242843, 31.113123 ], [ -86.239295, 31.111936 ], [ -86.234558, 31.113033 ], [ -86.229930, 31.113282 ], [ -86.227789, 31.113761 ], [ -86.228754, 31.115114 ], [ -86.227875, 31.116330 ], [ -86.226388, 31.119954 ], [ -86.226976, 31.124356 ], [ -86.227992, 31.127524 ], [ -86.227861, 31.133621 ], [ -86.228742, 31.136989 ], [ -86.230476, 31.140472 ], [ -86.226265, 31.138892 ], [ -86.224587, 31.139466 ], [ -86.222761, 31.142581 ], [ -86.219177, 31.142210 ], [ -86.216690, 31.144961 ], [ -86.213940, 31.144114 ], [ -86.208191, 31.142960 ], [ -86.206649, 31.142020 ], [ -86.201335, 31.140712 ], [ -86.198419, 31.138892 ], [ -86.193191, 31.137770 ], [ -86.193177, 31.190277 ], [ -86.193476, 31.192213 ], [ -86.194002, 31.211773 ], [ -86.193542, 31.213028 ], [ -86.193847, 31.219627 ], [ -86.193676, 31.228766 ], [ -86.193888, 31.283526 ], [ -86.189874, 31.283482 ], [ -86.190098, 31.290410 ], [ -86.189817, 31.297643 ], [ -86.190103, 31.301524 ], [ -86.184821, 31.301016 ], [ -86.185076, 31.302168 ], [ -86.186997, 31.304466 ], [ -86.191267, 31.305689 ], [ -86.194248, 31.304997 ], [ -86.194459, 31.318053 ], [ -86.193125, 31.317861 ], [ -86.190813, 31.319956 ], [ -86.190407, 31.323535 ], [ -86.172928, 31.323728 ], [ -86.169240, 31.321991 ], [ -86.169148, 31.317003 ], [ -86.165470, 31.316297 ], [ -86.163667, 31.316601 ], [ -86.153412, 31.316781 ], [ -86.152609, 31.316573 ], [ -86.151050, 31.313998 ], [ -86.143012, 31.306997 ], [ -86.142158, 31.305455 ], [ -86.139119, 31.309366 ], [ -86.137571, 31.310229 ], [ -86.137927, 31.311350 ], [ -86.140170, 31.314038 ], [ -86.140090, 31.315565 ], [ -86.142068, 31.320100 ], [ -86.141557, 31.322432 ], [ -86.142354, 31.324810 ], [ -86.142350, 31.327074 ], [ -86.143255, 31.327781 ], [ -86.143358, 31.330091 ], [ -86.144077, 31.330594 ], [ -86.143855, 31.335279 ], [ -86.142384, 31.336742 ], [ -86.142280, 31.337970 ], [ -86.135881, 31.337594 ], [ -86.131327, 31.338888 ], [ -86.131448, 31.334637 ], [ -86.132710, 31.331145 ], [ -86.133547, 31.327547 ], [ -86.134386, 31.319986 ], [ -86.126361, 31.321018 ], [ -86.120692, 31.322646 ], [ -86.116923, 31.324281 ], [ -86.113096, 31.324118 ], [ -86.102888, 31.324680 ], [ -86.101666, 31.326532 ], [ -86.100049, 31.326166 ], [ -86.098697, 31.324170 ], [ -86.099191, 31.322518 ], [ -86.101672, 31.319419 ], [ -86.104005, 31.315404 ], [ -86.105124, 31.311577 ], [ -86.105017, 31.309744 ], [ -86.102932, 31.306467 ], [ -86.102719, 31.305459 ], [ -86.104905, 31.300922 ], [ -86.108373, 31.297002 ], [ -86.107464, 31.294138 ], [ -86.109786, 31.294137 ], [ -86.110826, 31.293541 ], [ -86.113334, 31.290675 ], [ -86.116731, 31.292199 ], [ -86.121752, 31.292391 ], [ -86.124078, 31.294478 ], [ -86.126272, 31.290954 ], [ -86.126345, 31.281279 ], [ -86.125610, 31.278750 ], [ -86.121173, 31.278471 ], [ -86.118802, 31.276679 ], [ -86.117378, 31.277272 ], [ -86.115033, 31.275687 ], [ -86.115197, 31.272064 ], [ -86.115402, 31.268720 ], [ -86.116519, 31.262578 ], [ -86.116863, 31.257583 ], [ -86.117742, 31.254901 ], [ -86.119181, 31.252265 ], [ -86.119153, 31.250642 ], [ -86.120857, 31.248747 ], [ -86.123017, 31.247646 ], [ -86.124543, 31.245170 ], [ -86.125671, 31.245375 ], [ -86.126710, 31.247003 ], [ -86.128044, 31.246706 ], [ -86.128578, 31.245445 ], [ -86.126287, 31.242052 ], [ -86.126581, 31.240815 ], [ -86.129409, 31.239258 ], [ -86.132236, 31.239947 ], [ -86.133998, 31.236441 ], [ -86.133145, 31.235135 ], [ -86.134746, 31.234058 ], [ -86.135547, 31.232409 ], [ -86.136987, 31.232845 ], [ -86.137146, 31.234036 ], [ -86.138639, 31.234768 ], [ -86.140138, 31.232741 ], [ -86.140031, 31.231572 ], [ -86.136140, 31.224427 ], [ -86.134435, 31.223098 ], [ -86.133848, 31.221151 ], [ -86.129395, 31.221633 ], [ -86.126968, 31.219686 ], [ -86.126437, 31.217487 ], [ -86.122893, 31.218427 ], [ -86.121057, 31.221956 ], [ -86.121966, 31.224155 ], [ -86.121780, 31.225294 ], [ -86.116827, 31.224156 ], [ -86.111202, 31.224156 ], [ -86.109580, 31.225326 ], [ -86.108809, 31.226887 ], [ -86.108732, 31.223627 ], [ -86.104069, 31.222379 ], [ -86.099211, 31.222236 ], [ -86.098924, 31.217234 ], [ -86.097975, 31.212911 ], [ -86.094958, 31.207703 ], [ -86.093688, 31.203308 ], [ -86.095608, 31.202442 ], [ -86.096021, 31.200505 ], [ -86.093315, 31.200509 ], [ -86.088875, 31.195799 ], [ -86.085492, 31.193286 ], [ -86.116734, 31.193015 ], [ -86.116735, 31.183885 ], [ -86.114527, 31.183621 ], [ -86.113167, 31.183072 ], [ -86.111380, 31.181744 ], [ -86.110313, 31.180118 ], [ -86.109886, 31.178468 ], [ -86.109752, 31.176886 ], [ -86.108712, 31.176085 ], [ -86.107166, 31.175902 ], [ -86.104287, 31.175147 ], [ -86.101835, 31.174322 ], [ -86.099249, 31.173063 ], [ -86.097703, 31.173040 ], [ -86.096690, 31.172582 ], [ -86.095970, 31.168801 ], [ -86.096902, 31.167930 ], [ -86.096928, 31.166440 ], [ -86.096342, 31.165753 ], [ -86.094449, 31.164653 ], [ -86.094315, 31.163553 ], [ -86.094927, 31.160780 ], [ -86.095913, 31.158352 ], [ -86.096019, 31.156725 ], [ -86.094765, 31.154548 ], [ -86.094844, 31.153310 ], [ -86.095216, 31.151041 ], [ -86.092658, 31.150973 ], [ -86.091859, 31.150148 ], [ -86.092152, 31.149002 ], [ -86.093084, 31.148383 ], [ -86.094869, 31.147810 ], [ -86.096414, 31.147764 ], [ -86.100679, 31.148129 ], [ -86.101691, 31.147808 ], [ -86.101664, 31.147258 ], [ -86.100437, 31.146136 ], [ -86.098998, 31.145723 ], [ -86.096919, 31.144532 ], [ -86.096146, 31.142493 ], [ -86.096092, 31.141072 ], [ -86.097664, 31.141232 ], [ -86.099183, 31.140796 ], [ -86.099662, 31.139971 ], [ -86.098196, 31.137153 ], [ -86.098062, 31.136396 ], [ -86.098675, 31.135709 ], [ -86.100566, 31.135342 ], [ -86.101285, 31.134860 ], [ -86.100592, 31.133028 ], [ -86.100990, 31.131263 ], [ -86.102482, 31.129475 ], [ -86.102721, 31.127779 ], [ -86.102427, 31.126404 ], [ -86.101598, 31.124767 ], [ -86.100772, 31.123989 ], [ -86.099123, 31.121452 ], [ -86.098515, 31.118351 ], [ -86.097270, 31.115700 ], [ -86.097616, 31.113934 ], [ -86.097517, 31.116083 ], [ -86.098709, 31.118378 ], [ -86.100104, 31.117825 ], [ -86.101116, 31.117664 ], [ -86.104019, 31.116678 ], [ -86.105325, 31.116677 ], [ -86.107616, 31.117708 ], [ -86.110014, 31.118028 ], [ -86.111375, 31.119518 ], [ -86.111092, 31.113066 ], [ -86.113216, 31.113245 ], [ -86.115630, 31.113954 ], [ -86.119814, 31.116223 ], [ -86.119834, 31.113827 ], [ -86.119600, 31.112975 ], [ -86.117903, 31.109063 ], [ -86.117686, 31.108206 ], [ -86.117336, 31.104482 ], [ -86.116972, 31.103435 ], [ -86.115325, 31.101213 ], [ -86.114259, 31.099305 ], [ -86.113410, 31.094656 ], [ -86.112499, 31.091906 ], [ -86.111273, 31.090083 ], [ -86.114434, 31.089490 ], [ -86.114946, 31.090852 ], [ -86.115915, 31.091514 ], [ -86.118624, 31.090055 ], [ -86.119799, 31.088421 ], [ -86.120641, 31.086413 ], [ -86.122825, 31.084048 ], [ -86.123215, 31.083206 ], [ -86.124664, 31.081808 ], [ -86.126619, 31.081725 ], [ -86.127690, 31.079459 ], [ -86.129215, 31.077865 ], [ -86.130110, 31.077143 ], [ -86.130225, 31.076360 ], [ -86.129655, 31.075361 ], [ -86.127905, 31.074704 ], [ -86.126338, 31.072748 ], [ -86.125139, 31.071459 ], [ -86.126475, 31.070656 ], [ -86.125258, 31.070187 ], [ -86.125911, 31.069039 ], [ -86.124569, 31.068718 ], [ -86.124476, 31.067541 ], [ -86.123703, 31.067052 ], [ -86.123708, 31.065991 ], [ -86.125198, 31.066352 ], [ -86.124378, 31.065022 ], [ -86.123420, 31.063939 ], [ -86.122167, 31.063857 ], [ -86.121662, 31.062576 ], [ -86.121750, 31.060827 ], [ -86.122375, 31.059908 ], [ -86.123417, 31.060452 ], [ -86.124146, 31.061095 ], [ -86.124127, 31.061766 ], [ -86.125380, 31.061649 ], [ -86.126204, 31.061991 ], [ -86.128083, 31.061023 ], [ -86.127380, 31.060160 ], [ -86.129003, 31.060298 ], [ -86.130318, 31.061899 ], [ -86.131865, 31.061658 ], [ -86.133277, 31.061438 ], [ -86.134356, 31.061725 ], [ -86.134637, 31.062390 ], [ -86.136126, 31.062864 ], [ -86.137403, 31.063712 ], [ -86.138675, 31.063239 ], [ -86.140208, 31.063658 ], [ -86.140004, 31.062871 ], [ -86.143052, 31.062592 ], [ -86.145914, 31.062191 ], [ -86.145889, 31.061223 ], [ -86.147863, 31.061543 ], [ -86.147978, 31.062192 ], [ -86.149895, 31.061147 ], [ -86.150176, 31.060432 ], [ -86.151115, 31.060977 ], [ -86.152099, 31.061076 ], [ -86.152981, 31.062243 ], [ -86.153438, 31.063557 ], [ -86.152511, 31.064640 ], [ -86.149333, 31.065305 ], [ -86.148004, 31.065775 ], [ -86.145752, 31.067079 ], [ -86.144041, 31.068277 ], [ -86.141306, 31.069105 ], [ -86.136622, 31.069751 ], [ -86.134258, 31.069977 ], [ -86.132827, 31.069676 ], [ -86.133002, 31.079549 ], [ -86.133506, 31.081551 ], [ -86.133982, 31.084592 ], [ -86.133778, 31.087147 ], [ -86.134361, 31.088826 ], [ -86.135994, 31.090682 ], [ -86.137403, 31.093599 ], [ -86.138926, 31.092961 ], [ -86.141304, 31.093218 ], [ -86.141582, 31.094261 ], [ -86.147214, 31.094159 ], [ -86.147690, 31.093052 ], [ -86.148675, 31.092486 ], [ -86.150643, 31.092250 ], [ -86.150145, 31.092844 ], [ -86.151704, 31.093911 ], [ -86.152447, 31.094147 ], [ -86.159345, 31.094118 ], [ -86.159719, 31.091940 ], [ -86.160327, 31.090872 ], [ -86.159650, 31.089128 ], [ -86.159554, 31.086874 ], [ -86.158130, 31.085086 ], [ -86.157076, 31.084266 ], [ -86.152002, 31.081256 ], [ -86.150201, 31.080601 ], [ -86.146692, 31.080198 ], [ -86.146482, 31.079445 ], [ -86.146948, 31.079043 ], [ -86.150118, 31.078109 ], [ -86.151531, 31.077048 ], [ -86.152586, 31.075393 ], [ -86.154523, 31.073766 ], [ -86.155763, 31.073145 ], [ -86.156274, 31.072056 ], [ -86.158569, 31.070819 ], [ -86.159818, 31.069386 ], [ -86.162474, 31.069657 ], [ -86.166110, 31.071319 ], [ -86.166394, 31.074885 ], [ -86.171231, 31.074280 ], [ -86.172389, 31.075502 ], [ -86.172906, 31.076695 ], [ -86.174715, 31.078868 ], [ -86.175392, 31.080716 ], [ -86.176235, 31.081250 ], [ -86.178312, 31.081987 ], [ -86.179149, 31.083274 ], [ -86.179622, 31.085502 ], [ -86.179609, 31.087102 ], [ -86.180388, 31.090925 ], [ -86.181487, 31.092465 ], [ -86.182238, 31.093948 ], [ -86.183178, 31.092799 ], [ -86.185532, 31.090407 ], [ -86.192334, 31.090363 ], [ -86.197872, 31.091541 ], [ -86.213719, 31.094105 ], [ -86.215331, 31.094573 ], [ -86.228515, 31.099433 ], [ -86.233797, 31.100266 ], [ -86.242899, 31.100547 ], [ -86.244802, 31.099929 ], [ -86.248192, 31.097862 ], [ -86.252549, 31.097354 ], [ -86.252678, 31.099018 ], [ -86.254747, 31.102346 ], [ -86.255008, 31.103419 ] 
    ], 
    [ 
        [ -86.184821, 31.301016 ], [ -86.184729, 31.300839 ], [ -86.184459, 31.298498 ], [ -86.181177, 31.294075 ], [ -86.178058, 31.292649 ], [ -86.169179, 31.291602 ], [ -86.162755, 31.287625 ], [ -86.158862, 31.289840 ], [ -86.156081, 31.292407 ], [ -86.155109, 31.295007 ], [ -86.156611, 31.296646 ], [ -86.158631, 31.297008 ], [ -86.165275, 31.300376 ], [ -86.170899, 31.306743 ], [ -86.175389, 31.308973 ], [ -86.178368, 31.306745 ], [ -86.181431, 31.302192 ], [ -86.184598, 31.301013 ], [ -86.184821, 31.301016 ] 
    ] 
] 

如您所见,数组看起来像这样:

[0]
    [0]
        [0]
            [0] => coordinate 1 Latitude
            [1] => coordinate 1 Longitude
            .....
        [40]
            [0] => coordinate 41 Latitude
            [1] => coordinate 41 Longitude
    [1]
        [0]
            [0]
                [0] => coordinate 1 Latitude
                [1] => coordinate 1 Longitude
                .....
            [385]
                [0] => coordinate 386 Latitude
                [1] => coordinate 386 Longitude
        [1]
            [0]
                [0] => coordinate 1 Latitude
                [1] => coordinate 1 Longitude
                .....
            [18]
                [0] => coordinate 19 Latitude
                [1] => coordinate 19 Longitude

我希望创建一个递归函数,它将钻取这样一个时髦的数组,基本上只返回带有三个子项的多维数组中的坐标。我尝试了一些事情但是甚至没有接近。

1 个答案:

答案 0 :(得分:0)

因为我知道最深层次的坐标,我认为应该这样做......

function cleanArray($coords)
{
    $toReturn = array();

    foreach ($coords as $thisCoords)
    {
        foreach ($thisCoords as $finalCoords)
        {
            $testArray = $finalCoords[0];
            $lastArray = $finalCoords;

            while (is_array($testArray[0]))
            {
                $lastArray = $testArray;
                $testArray = $testArray[0];
            }

            $toReturn[] = $lastArray;
        }
    }

    return $toReturn;
}