PHP Associative Arrays

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

Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up