Return query generate Array

Asked

Viewed 16 times

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 this 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),
);

1 answer

2


It is necessary to create an array before the 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 );

Demonstration

  • Thank you, perfect!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.