I believe we could change the values of an array by reference in a foreach loop like this foreach ($arr as $key => &$value)
I want to modify the keys in my case
$input = array(32 => 2, 99 => 4, 100 => 4);
foreach ($input as &$key => $value)
{
$key = chr($key); // I want to change the ascii character to a letter
}
I got an error saying, we cannot pass keys by reference. Any suggestions?
Try this if this works:
foreach($input as $key => $val)
{
$key[] = [(string)$key => $val];
}
print_r($key);