How can the use of array indexes in PHP be optimized for efficient data retrieval?

When using array indexes in PHP for efficient data retrieval, it is important to ensure that the indexes are properly optimized to minimize the time complexity of data access. One way to achieve this is by using associative arrays instead of numeric indexes, as associative arrays provide faster data retrieval based on keys. Additionally, sorting arrays based on keys can also improve efficiency when accessing data.

// Example of using associative arrays for efficient data retrieval
$data = [
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
];

// Accessing data using associative array keys
echo $data['key2']; // Output: value2