<?php
$arr1 = Array('Apple', 'Ball', 'whale', 'Dog', 'cat');
$arr2 = Array('Red apple' => 27, 'tball' => 28, 'cat' => 29, 'Dog' => 30, 'blue ball' => 31, 'dog food' => 25, 'green apple' => 111);
$keys = array_keys($arr2);
foreach($arr1 as $find){
$matches = preg_grep("/\b" . preg_quote($find) . "/i", $keys);
foreach($matches as $key => $word){
$result[$find][] = $arr2[$word];
}
}
var_dump($result);
- 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.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
- array(4) {
["Apple"]=>
array(2) {
[0]=>
int(27)
[1]=>
int(111)
}
["Ball"]=>
array(1) {
[0]=>
int(31)
}
["Dog"]=>
array(2) {
[0]=>
int(30)
[1]=>
int(25)
}
["cat"]=>
array(1) {
[0]=>
int(29)
}
}
- 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(4) {
["Apple"]=>
array(2) {
[0]=>
int(27)
[1]=>
int(111)
}
["Ball"]=>
array(1) {
[0]=>
int(31)
}
["Dog"]=>
array(2) {
[0]=>
int(30)
[1]=>
int(25)
}
["cat"]=>
array(1) {
[0]=>
int(29)
}
}
preferences:
193.83 ms | 402 KiB | 183 Q