Arrays may have non-numeric indexes.
Data can be inserted by specifying certain key and value.
Example:
In this example, 2 records have been inserted.
Values can be accessed via their keys.
Let's look at the following example:
Output:
The key-value pair for Coke is set at line 4 and accessed at line 7.
Coke
line 4
line 7
Accessing key that's not set in the array results in warning and null value.
The array doesn't contain any Coke key, so $drinks["Coke"] has a value of null.
$drinks["Coke"]
Let's have a program in which students scores are stored in array and printed afterwards.
That's done via the unset function like in this example:
unset
It can be done via the isset function like in the following code:
isset
Water key is not set thus Water is not printed.
Water
Size is the count of the key-value pairs.
It can be accessed via the count function.
count
Let's look at this example:
4 records were inserted and 1 is deleted afterwards. This makes for 3 key-value pairs.