In the products array, we allowed PHP to give each item the default index. This meant that the first item we added became item 0, the second item 1, and so on.
In addition to numerically indexed arrays, PHP also supports the second type of array, which is known as an associative array. Create an associative array follows the same steps as creating a numerically indexed array, and the process is the same as it was previously. We can declare a new variable and then assign the contents of that variable to the value returned by the array function when we call it. That array that is produced at that time has the capability of being utilised as either a numerically indexed array or as this new type of array, an associative array, depending on the situation.
When compared to numerically indexed arrays, the way an associative array works is that instead of utilising an index number to indicate a particular member of the array, associative arrays instead use other values, most frequently strings to indicate a particular element of the array.
Essentially, the only real fundamental difference between a binary and an associative array is that, instead of using a number to represent a value, we are using strings, or other values, something other than a simple integer, to be able to indicate a specific position, a specific value that is stored in an array. This can be quite useful because it’s similar to having a single array that has two sets of data that are related in some way, which is why I think they’re referred to as associative arrays.
Benefits of Associative Arrays
In our program/script, the following are some of the advantages of using an associative array:
- With more meaningful key or index values, we may make our array elements more meaningful.
- Using an array element with a key and value, we can store more information since we can use a string as the key to the array element and store data connected with the value to be stored. In our example, we used a string key to store the type of car and the name of the automobile as the value. As a result, one array element can store two different pieces of information.