1

I have a multidimensional array that I would like to loop through and print the values that are stored in the array. This is the end result I am looking to achieve

{ "lat": 52.4469601, "lon": -1.93685532},
{ "lat": 52.44332417, "lon": -1.9426918},
{ "lat": 52.43987106, "lon": -1.9329071}

How would I go about printing the values like this? Currently this is how I am printing the whole array:

$enc = 'NTIuNDQ2OTYwMSwtMS45MzY4NTUzMnw1Mi40NDMzMjQxNywtMS45NDI2OTE4fDUyLjQzOTg3MTA2LC0xLjkzMjkwNzF8NTIuNDQ1NDk1MywtMS45MjU4MjYwN3w';

$decoded = base64_decode($enc);

$trim = trim($decoded, '|');
$data = explode('|', $decoded);
$out = array();
$step = 0;
$last = count($data);
$last--;

foreach( $data as $key => $item ) {
    foreach (explode(',', $item) as $value) {
        $out[$key][] = $value;
    }
}

echo "<pre>";
print_r( $out );
echo "</pre>";

And the output looks like:

Array
(
    [0] => Array
        (
            [0] => 52.4469601
            [1] => -1.93685532
        )

    [1] => Array
        (
            [0] => 52.44332417
            [1] => -1.9426918
        )

    [2] => Array
        (
            [0] => 52.43987106
            [1] => -1.9329071
        )

    [3] => Array
        (
            [0] => 52.4454953
            [1] => -1.92582607
        )

)
9
  • I suppose the last code is how your array is built initialy right? Commented Mar 24, 2017 at 10:55
  • print input arr Commented Mar 24, 2017 at 10:55
  • @JuOliveira yes that is how the array is built :) Commented Mar 24, 2017 at 10:56
  • 1
    instead of print_r use echo json_encode($out); ? Commented Mar 24, 2017 at 10:56
  • 2
    json_encode is your friend Commented Mar 24, 2017 at 10:57

3 Answers 3

1

If your data is exported always with "lat" and "lon" pairs then you can do this:

foreach( $data as $key => $item ) {
    $lat = true;
    foreach (explode(',', $item) as $value) {
        if($lat == true){
            $out[$key]["lat"] = $value;
            $lat = false;
        } else {
            $out[$key]["lon"] = $value;
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome that was pretty much what I was looking for - along with adding json_encode like @Ann-SophieAngermüller and @AlexOdenthal suggest worked fine
0

An other way to do it using a stream approach:

$enc = 'NTIuNDQ2OTYwMSwtMS45MzY4NTUzMnw1Mi40NDMzMjQxNywtMS45NDI2OTE4fDUyLjQzOTg3MTA2LC0xLjkzMjkwNzF8NTIuNDQ1NDk1MywtMS45MjU4MjYwN3w';

$handle = fopen("data:text/plain;base64,$enc", 'r');

$res = [];    

while ( false !== $rec = stream_get_line($handle, 0, '|') ) {
     $res[] = array_combine(['lat', 'lon'], str_getcsv($rec));
}

echo json_encode($res);

Comments

0
$enc = 'NTIuNDQ2OTYwMSwtMS45MzY4NTUzMnw1Mi40NDMzMjQxNywtMS45NDI2OTE4fDUyLjQzOTg3MTA2LC0xLjkzMjkwNzF8NTIuNDQ1NDk1MywtMS45MjU4MjYwN3w';

$decodedArr = explode('|', base64_decode($enc));

$latLong = [];
foreach ($decodedArr as $latLongStr) {
    if (!$latLongStr) {
        continue;
    }

    $temp = explode(',', $latLongStr);

    $latLong[] = ['lat' => $temp[0], 'lon' => $temp[1]];
}

echo json_encode($latLong);

Output

[{"lat":"52.4469601","lon":"-1.93685532"},{"lat":"52.44332417","lon":"-1.9426918"},{"lat":"52.43987106","lon":"-1.9329071"},{"lat":"52.4454953","lon":"-1.92582607"}]

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.