3v4l.org

run code in 300+ PHP versions simultaneously
<?php $a = Array ( 'cap' => 3, 'shirt' => 2, 'tatuaggio' => 1, 'badge' => 2 ); $res=[]; foreach($a as $k => $v){ $res[] = array_fill(0, $v, $k); } print_r(array_merge(...$res));// splat operator echo "----------------Without Splat operator -------------------------------------"; $res=[]; foreach($a as $k => $v){ $res = array_merge($res,array_fill(0, $v, $k)); } print_r($res); echo "----------------With foreach and for -------------------------------------"; $res=[]; foreach($a as $k => $v){ for($i=0;$i<$v;$i++) $res[] = $k; } print_r($res);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.14, 8.4.16 - 8.4.17, 8.5.0 - 8.5.2
Array ( [0] => cap [1] => cap [2] => cap [3] => shirt [4] => shirt [5] => tatuaggio [6] => badge [7] => badge ) ----------------Without Splat operator -------------------------------------Array ( [0] => cap [1] => cap [2] => cap [3] => shirt [4] => shirt [5] => tatuaggio [6] => badge [7] => badge ) ----------------With foreach and for -------------------------------------Array ( [0] => cap [1] => cap [2] => cap [3] => shirt [4] => shirt [5] => tatuaggio [6] => badge [7] => badge )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
179.56 ms | 408 KiB | 5 Q