Creating an Array from database table

I’m trying to create an array from the data pulled from my database. I want the array to be arranged like the example shown below:

Example:
$code_sections = array (
‘Player Setup’ => ‘player_setup’,
‘Land Generation’ => ‘land_generation’,
‘Elevation Generation’ => ‘elevation_generation’,
‘Cliff Generation’ => ‘cliff_generation’,
‘Terrain Generation’ => ‘terrain_generation’,
‘Connection Generation’ => ‘connection_generation’,
‘Object Generation’ => ‘object_generation’,
);

The data I’m trying to pull from the database is sorted in the following way:

Is there a way to arrange it like shown in the example?

Why don’t you just fetch the data as an array from the MySQL Database Table?
https://www.php.net/manual/en/pdostatement.fetchall.php

Then just use one of many PHP array functions to sort the way you want the data to look.

Do you think you can make a concrete example? I’m struggling to make it work.

Are you asking how to parse array keys and data? Something loosely like this should work…

<?php
$code_sections = array (
   "Player Setup" => "player_setup",
   "Land Generation" => "land_generation",
   "Elevation Generation" => "elevation_generation",
   "Cliff Generation" => "cliff_generatio",
   "Terrain Generation" => "terrain_generation",
   "Connection Generation" => "connection_generation",
   "Object Generation" => "object_generation",
);
echo "<table><th><tr><td>Name</td><td>Code</td></tr></th>";
foreach ($code_sections as $name=>$code) {
    echo "<tr><td>".$name."</td></td>".$code."</td></tr>";
}
echo "</table>";
?>

But, if the array is saved in a database, the data can be retrieved and displayed in a similar fashion.

Sponsor our Newsletter | Privacy Policy | Terms of Service