0

I'm try to print an array where every other value is reassigned, as examples (from this):

17.34502870451717,62.46137370987033

To this:

62.46137370987033,17.34502870451717

That part have I succeeded with, but now I have this structure:

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

This is where I get stuck and do not know how to write.

The structure I want looks like this:

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

Here is my explode.php (GIST)

<?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;
?>
2
  • 1
    Your "now I have this structure" does not look well, it seems to be missing end brackets. Commented Jul 29, 2013 at 9:58
  • @JoachimIsaksson That's because I have a "The structure I want.." If I didn't misunderstood you. Commented Jul 29, 2013 at 9:59

3 Answers 3

1

As I understand it, as long as there's always even pairs it should be as easy as;

<?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]
Sign up to request clarification or add additional context in comments.

Comments

1

Try This

 $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>";              
 } 

Comments

1

Just a small modification to the given answers

$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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.