How can I change the following code so the keys
of $result
will not get overwritten by [0], [1], ...
but instead be kept the way they were?
$result = array_map(function($key, $value) {
return preg_replace("/(".$_POST['keyword'].")/i", "<mark>$1</mark>", $value);
}, array_keys($result), $result);
CodePudding user response:
Use array_walk
(docs) instead of array_map
it will preserve keys:
array_walk($result, function(&$key, $value) {
$key = preg_replace("/(".$_POST['keyword'].")/i", "<mark>$1</mark>", $value);
}, $result);