0

我正在尝试打印一个数组,其中所有其他值都被重新分配,例如(来自this):

17.34502870451717,62.46137370987033

对此:

62.46137370987033,17.34502870451717

那部分我已经成功了,但现在我有了这个结构:

[62.46137370987033,[17.34501402936927,]
[62.46123453616544,[17.34525377433593,]
[62.4610178881864,[17.34546663705899,]

这就是我卡住了,不知道怎么写的地方。

我想要的结构如下所示:

[62.392628, 17.309413],
[62.393162, 17.309193],
[62.393403, 17.30922]

这是我的explode.phpGIST

<?php
        $dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
        $minion = explode(",",$dwarf); 
        $wing = "[";
        for ($i = 0;$i < count($minion) -1; $i++) {
                echo $wing . $minion[$i+1].",";
                if($i%2==1) { echo "]<br />"; }
        } echo $minion[0] . $wing;
?>
4

3 回答 3

1

据我了解,只要总是有偶数对,它就应该很容易;

<?php
    $dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
    $minion = explode(",",$dwarf);
    $eol = '';
    for ($i = 0;$i < count($minion) -1; $i+=2) {
       echo $eol.'['.$minion[$i+1].','.$minion[$i]."]";
       $eol=',<br/>';
    }
    echo '<br/>';

>>> [62.46137370987033,17.34502870451717],
>>> [62.46123453616544,17.34501402936927]
于 2013-07-29T10:05:27.357 回答
1

只是对给定答案的一个小修改

$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf); 
$str = '';
    for ($i = 0;$i < count($minion) -1; $i+=2) {
        $str.='['.$minion[$i+1].','.$minion[$i].'],<br/>';
    } 
echo rtrim($str,','); // to trim ',' at the end
于 2013-07-29T11:00:35.597 回答
1

尝试这个

 $dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
 $minion = explode(",",$dwarf); 
 $wing = "[";
 for ($i = 0;$i < count($minion) -1; $i+=2) 
 {
       echo  $kk =   $wing . $minion[$i+1].",".$minion[$i]."],<br>";              
 } 
于 2013-07-29T10:43:23.677 回答