How to create a dynamic associative array using PHP for loop and MySQL

Creating and populating an associative array from database in PHP is very simple. All you have to do is connect to MySQL database, select data from the table, use a for loop and push data into an array. I will show an example how you can do it.

I have displayed data from database in a html table below and then same data is displayed from an associative array after populating it using PHP and MySQL.

populate associative array in php

All I am doing here is just select data from database table, display it row by row in a html table and populate the associative array at the same time. Then display data from the array just to make sure array is poulated correctly.

I have a database table called 'languages'. In my program I created a database connection php program (dbconnect.php) and an index.php which will show all language ids and names from a database table and then display them from the associative array after it is populated.

Create the table

Let us create a table named 'languages' in MySQL database. This table will have name of some programming languages. I have a database called 'demo'. So 'languages' table will be created in this database. If you have an existing database other than demo you can also use it. Just make sure same table name does not exist already. Table structure is given below:

php array key value in associative array

Table has 2 columns.

  1. id - it is the primary key and auto incremented
  2. language - programming language name
Create table script for this table is given below, you can use this code to create the table. You can also download (download section later in this topic) and get the scripts.


After you run the scripts, verify if data is inserted correctly, it should have below data in it:

Dynamically populate an associative array in PHP

Connect to MySQL database (dbconnect.php)

Use below script to connect to the database. Note that we have this database connection php program. This is written in a separate php program as it can be used in every program where database connection is needed. This will be easier for maintenance and also will enable reusability of the code.


I am using mysqli_connect() function which needs 4 parameters.

  1. server - in our case it is localhost
  2. userid - we are using root user
  3. password - no password for user root
  4. database name - demo in our case.
If connection is successful then it will return true and false otherwise. This Boolean value is returned and stored in the $conn variable. We will include this dbconnect.php in other php programs so that we do not need to write it again in the program. For detail database connection understanding please read topic How to Connect to MySQL database using PHP.

PHP program to populate the array and display data (index.php)

This is the program which populates the programming language ids and names from database to an associative array using key=>value pair and also display all data from database as well as from the array. So, the array will have data in pair, like $id=>$language


An array is declared named $languages. this array will be populated with language ids and names like 1=>Java, 2=>C++, 3=>Python etc.

Next, just to make sure I have got all language ids and names in the array, I am displaying all elements from the array.

I created a folder called 'assoc_array' under xampp/htdocs and saved index.php and other files in that folder.

I have few simple styles, so I put together in a file named style.css. See below:


Now, if you want to test it, just make sure in your XAMPP control panel Apache and MySQL services are running. Open the browser and run localhost/assoc_array. You will see the below page displayed:

create associative array in php using for loop

First, all languages are displayed along with their ids from database table and then, language ids and names are displayed from the associative array. You can see all languages from database table are in the array.

build dynamic associative array from database in phpImportant Note

You can also use var_dump() to display data from the array. Here, I also demonstrated how you can access an associative array to display data using a for loop.

Dynamically create Associative array using loopDownload Source Code

I have put all codes in a zip file. You can download it by clicking on the Download button below. You do not need to register yourself to download it. You can directly use the code or you can modify them as per your requirements.

populate associative array phpConclusion

To do some processing on the data from database, you can quickly get it in an array and apply array functions or string functions available in PHP. Just remember that if the data volume is too high, then array processing can be slow and it can take lot of memory. Otherwise, array processing is very fast. Hope you could understand the way I have presented it using this simple application.