I need to create an array like this:
['A1','A2','A3','A4','B1','B2','B3','B4','C1','C2','C3','C4'];
In php you can do this:
$letters = ['A', 'B', 'C'];
$arr = [];
foreach($letters as $l){
for($i=1;$i<=4;$i++){
$str = $l . '' . $i;
array_push($arr, $str);
}
}
Isn’t there a cleaner way to do this?
3
Answers
Sure,
array_push
is pretty old school 😛 This is cleaner.It is not much cleaner, but I could come up with this:
For sure! To avoid pushing elements in a mutable array, you could do this :
See ? So functional, so elegant.
No, your way is better. Maybe get rid of the unneeded
$str
variable, but otherwise it’s about as readable as it could be.