Home > Net >  Keep the indexes on an array
Keep the indexes on an array

Time:04-16

How can I change the following code so the keysof $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);
  • Related