3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr1 = ['word', 'chord', 'tech', 'care', 'kek', 'lol', 'wild', 'regex']; $arr2 = ['ord', 'ek', 'ol', 'ld', 'gex', 'ss']; $keys=[]; foreach($arr2 as $val){ $matches = preg_grep("/.+" . preg_quote($val) . "/", $arr1); $keys = array_merge($keys, array_keys($matches)); // save keys of matched words foreach($matches as $key => $m) $new[$val][] = str_replace($val, "_$val", $arr1[$key]); } $new['unmatched'] = array_diff_key($arr1, array_flip($keys)); // add unmatched words var_dump($new);
Output for 7.1.0 - 7.1.33, 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.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array(6) { ["ord"]=> array(2) { [0]=> string(5) "w_ord" [1]=> string(6) "ch_ord" } ["ek"]=> array(1) { [0]=> string(4) "k_ek" } ["ol"]=> array(1) { [0]=> string(4) "l_ol" } ["ld"]=> array(1) { [0]=> string(5) "wi_ld" } ["gex"]=> array(1) { [0]=> string(6) "re_gex" } ["unmatched"]=> array(2) { [2]=> string(4) "tech" [3]=> string(4) "care" } }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array(6) { ["ord"]=> array(2) { [0]=> string(5) "w_ord" [1]=> string(6) "ch_ord" } ["ek"]=> array(1) { [0]=> string(4) "k_ek" } ["ol"]=> array(1) { [0]=> string(4) "l_ol" } ["ld"]=> array(1) { [0]=> string(5) "wi_ld" } ["gex"]=> array(1) { [0]=> string(6) "re_gex" } ["unmatched"]=> array(2) { [2]=> string(4) "tech" [3]=> string(4) "care" } }

preferences:
181.33 ms | 403 KiB | 196 Q