I am Learn.i.ng

How to efficiently loop through PHP array elements

Use case: you have an array of images to which you want to prepend a path to get the full URL to be sent as an API response.


$images = array("image1.png", "image2.png", "image3.png", "image4.png", "image5.png");

A traditional way of doing things will be looping through each element of the array, prepending the path to each element, pushing the modified element to another array and returning that array.