PHP Associative Arrays

Post

Basic primer on associative arrays in php.

To create an associative array, define it all at once like this:

$arr = array(
'key' => 'value',
'otherkey' => 1,
);

or you can assign one value at a time

$arr = array();
$arr['key'] = 'value';
$arr['otherkey'] = 1;

You can always add or change a current value using:

$arr['newkey'] = 2;
$arr['key'] = 1;

Remember if you use array sort functions to use the ones that keep the associative keys intact like: uasort(), ksort(), and asort()

You can use associative arrays for many things, from holding simple data to multiple nested levels for configuration.

Comments

Leave a comment

November 2026
Sun
Mon
Tue
Wed
Thu
Fri
Sat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Share