Does foreach() work for non-numerical array keys?

与世无争的帅哥 提交于 2019-12-02 13:24:44

Yes, foreach supports any kind of key. In your case, $key will be a string, 'eggs' and 'pencil' respectively for each item. In fact, foreach was intended for use with arrays that have non-numerical keys which you can't easily iterate using for.

Yes, PHP has no real distinction between arrays with numeric vs non-numeric keys. They're all simply arrays as far as PHP is concerned.

Yes the explanation given by BoltClock is right & i would suggest you to manually try too. You have missed $before array name in the foreach statement

foreach($arr_name as $key=>$value) echo $value ?>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!