12 December 2023

Utilizes preg_filter to add the specified prefix to each item in the array. There are other methods, but this is fast and a single-liner.

Source code viewer
  1. // Original array
  2. $array = array(0, 1, 2);
  3.  
  4. // Use preg_filter to add the prefix
  5. $prefixed_array = preg_filter('/^/', 'prefix_', $array);
  6.  
  7. // Display the modified array
  8. print_r($prefixed_array);
Programming Language: PHP