Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!