On older PHP versions I could do the following.
$arr = ['foo', 'bar'];
var_dump($arr);
foreach ($arr as $i => $v) {
$arr[$i]['string'] = 'baz';
}
Now when I'm doing such an operation in PHP7, it fails with the following error:
Illegal string offset 'string' [sample.php, line 4]
Why is this and why can't I do this anymore? I already found a lot of answers about typecasting in PHP7, that it's not that graceful anymore, so I suspect it has to do with that, but I can't find my answer on the web. Am I missing something?
The var_dump result from line 2
array (size=2) 0 => string 'boo' (length=3) 1 => string 'bar' (length=3)
after the foreach I was expecting the following result
array (size=2)
'foo' =>
array (size=1)
'string' => string 'baz' (length=3)
'bar' =>
array (size=1)
'string' => string 'baz' (length=3)
$arr[$i]['string'] = 'baz';? Accessing the fieldstringof a string doesn't seem logical at all. Why not$arr[$i] = ['string' => 'baz'];? edit: Looking closer at your expected output, I'm even more perplexed. How do you expect the value of the array to suddenly become keys? Long story short, that's simply not how PHP works.