12 December 2023

Using preg_filter to add a suffix ("_suffix") to each item in the array. Use this one-liner to for good performance and easy solution. There are other solutions, but this is the preferred one.

Source code viewer
  1. // Original array
  2. $array = array("item1", "item2", "item3");
  3.  
  4. // Use preg_filter to add the suffix
  5. $suffixed_array = preg_filter('/$/', '_suffix', $array);
  6.  
  7. // Display the modified array
  8. print_r($suffixed_array);
Programming Language: PHP