Your code works correctly, if you're looking for a more elegant way I would either suggest a bash script or looking at the array_map function (possibly other ones like array_walk, array_filter too.)
That being said, your code is simple and easy to follow so probably the best way to go.