Query return generate Array

0

I have a query in MySQL.

foreach($resultado as $row)
{
$lat = $row['lat'];
$lon = $row['lon'];
$horario = $row['horario'];
}

I need the result to mount an array similar to the one below.

$markers = array(
array("latitude" => $lat, "longitude" => $lon, "conteudo" => $horario),
array("latitude" => $lat, "longitude" => $lon, "conteudo" => 'aaaaa'),
array("latitude" => -22.688211, "longitude" => -47.311065, "conteudo" => 
$horario),
);
    
asked by anonymous 19.01.2018 / 15:04

1 answer

2

You need to create an array before foreach and then create a multidimensional array .

<?php

$resultado = [
["lat" => 0.0,"lon" => 0.0,"horario" => "00:00",],
["lat" => 0.0,"lon" => 0.0,"horario" => "06:00",],
["lat" => 0.0,"lon" => 0.0,"horario" => "12:00",],
["lat" => 0.0,"lon" => 0.0,"horario" => "18:00",],
["lat" => 0.0,"lon" => 0.0,"horario" => "24:00",]];

$result = [];

foreach($resultado as $row)
{
    $result[] = [
        "latitude" => $row["lat"],
        "longitude" => $row["lon"],
        "conteudo" => $row["horario"]
    ];
}

var_export( $result );

Demo

    
19.01.2018 / 15:13