<?php
$array = [
'a' => 'food',
'b' => 'foo',
'c' => 'goo',
'd' => 'foo',
'e' => 'good'
];
$needle = 'foo';
$newValue = 'bar';
function array_replace_value(&$ar, $value, $replacement)
{
$ar = preg_replace(
'/^' . preg_quote($value, '/') . '$/',
$replacement,
$ar
);
}
array_replace_value($array, $needle, $newValue);
var_export($array);
- Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- array (
'a' => 'food',
'b' => 'bar',
'c' => 'goo',
'd' => 'bar',
'e' => 'good',
)
preferences:
103.01 ms | 402 KiB | 89 Q