php change value array code example
Example 1: how to replace array key php
function array_replace_key($search, $replace, array $subject) {
$updatedArray = [];
foreach ($subject as $key => $value) {
if (!is_array($value) && $key == $search) {
$updatedArray = array_merge($updatedArray, [$replace => $value]);
continue;
}
$updatedArray = array_merge($updatedArray, [$key => $value]);
}
return $updatedArray;
}
$user = ['name' => 'Donald Pakkies', 'age' => 22];
var_dump(array_replace_key('name', 'full_name', $user));
Example 2: how to re assign value of associative array after assign in php
$employee = [
"bill" => 25,
"steve" => 36,
"alice"=>44
];
$employee["alice"] = 48;
$employee["musk"] = 42;
echo "<pre>";
print_r ($employee);
echo "</pre>";