3

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?

4
  • This isn't even valid syntax, you can't put a foreach inside an array element. You could use array_map() Commented Sep 17, 2021 at 16:22
  • What does this have to do with JavaScript or JSON? Commented Sep 17, 2021 at 16:23
  • I should have removed those tags, the original question had where I was returning the json_encoded version into a javascript constant, but I realized I just needed to solve this at the array building level. Checking out your answe3r now, but it's giving me a syntax error. I think I see why though Commented Sep 17, 2021 at 16:30
  • The ; in the middle of the array was wrong Commented Sep 17, 2021 at 16:31

1 Answer 1

3

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)
];
Sign up to request clarification or add additional context in comments.

3 Comments

After the update it still seems to be giving the unexpected `}, item` expectinvg `;` error
There was a missing semicolon, check the updated answer
Thanks so much, I got it working now! I'm also converting the logo and phone into proper formats

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.