Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By ParisLiakos on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Issue links:
Description:
drupal_map_assoc()
existed to work around a PHP bug with array_combine().
In PHP 5.4 this bug was fixed, which resulted in this function's removal in Drupal 8 as the same behavior can be achieved with array_combine() and array_map().
In earlier versions of Drupal 8 \Drupal\Component\Utitlity\MapArray::copyValuesToKeys()
existed for the same purpose. This method (and the containing class) has been removed, as well.
D7
$options = drupal_map_assoc($my_array);
// With callback
$options = drupal_map_assoc($my_array, $callback);
D8
$options = array_combine($my_array, $my_array);
// With callback
$options = array_map($callback, array_combine($my_array, $my_array));
Earlier D8 (no longer valid)
use Drupal\Component\Utility\MapArray;
$options = MapArray::copyValuesToKeys($my_array);
// With callback
$options = MapArray::copyValuesToKeys($my_array, $callback);
Impacts:
Module developers
Themers