Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
Looping off of MySQL result and building custom array

The following code is building the proper structure of array that I want, but it's only doing it for one result even though the MYSQL result is about 65 items.

I need the 'type' and 'features' levels to be static, and then start the loop within the features level (so every mysql result would build an item in within the features level)

The following is only loading the last result from the query:

$db->setQuery($query);
$item=$db->loadObjectList();
echo $db->getErrorMsg();
//dump($item);

$stores = [
    'type' => 'FeatureCollection',
    'features' => [
      foreach ($item as $i) {
        [
            'type' => 'Feature',
            'geometry' => [
                'type' => 'Point',
                'coordinates' => [
                    $i->lat,
                    $i->lng
                ]
            ],
            'properties' => [
                'storeImage' => $i->logo, // you need to convert this into an actual URL
                'storeName' => $i->name,
                'phoneFormatted' => $i->phone, // this needs formatting aswell
                'address' => $i->address,
                'city' => $i->city,
                'country' => $i->country,
                'postalCode' => $i->zip,
                'state' => $i->state
            ]
        ]
      }
    ]
];

How can I properly loop the mysql query to build my array in the correct way?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Use array_map() to generate a new array from another array.

$item=$db->loadObjectList();

$stores = [
    'type' => 'FeatureCollection',
    'features' => array_map(function($i) {
        return [
            'type' => 'Feature',
            'geometry' => [
                'type' => 'Point',
                'coordinates' => [
                    $i->lat,
                    $i->lng
                ]
            ],
            'properties' => [
                'storeImage' => $i->logo, // you need to convert this into an actual URL
                'storeName' => $i->name,
                'phoneFormatted' => $i->phone, // this needs formatting aswell
                'address' => $i->address,
                'city' => $i->city,
                'country' => $i->country,
                'postalCode' => $i->zip,
                'state' => $i->state
            ]
        ];
    }, $item)
];
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda