asmblah / uniter

🎉 PHP in the browser and Node.js => Docs: https://phptojs.com/
https://asmblah.github.io/uniter/
Other
446 stars 42 forks source link

wrong output in foreach loops #56

Closed abbychau closed 4 years ago

abbychau commented 4 years ago
<?php
$k=['test'=>'dafdsf'];
foreach($k as $v){
    echo $v['test'];
}
?>

expected: 'dafdsf' now: 'd'

asmblah commented 4 years ago

Hi @abbychau,

Is there a typo in your snippet above? I've just run this through both Zend's PHP engine and Uniter and it gives the same output, just d. In your snippet, on the first (and only) iteration of the loop $v will be dafdsf and so $v['test'] tries to fetch an element at index 'test' of that string, which raises a warning and just fetches d (as the string 'test' is cast to a 0, so it just fetches the character at index 0).

I've tried a similar snippet, wrapping the array again eg.

<?php
$k=[
  ['test'=>'dafdsf']
];
foreach($k as $v){
    echo $v['test'];
}
?>

and that gives the same output in both engines (dafdsf).

Hope this helps!

abbychau commented 4 years ago

ah, I made a mistake :p sorry