dongqi0644 2013-07-10 13:41
浏览 42

请有人解释一下这是什么在PHP?

everyone. I write this code in my test php. Once I execute in browser, it's just display the initial, the first letter of array. For further, here is my code:

$args = array ('computer'=>'pentium', 'notebook'=>'atom', 'gadget'=>'tab'); foreach ($args as $key => $val) {echo $key['val'];}

and the result in my browser is "cng" which I think c = computer, n = notebook, g = gadget.

so, would someone explain me how could it happens?

thanks in advance

  • 写回答

2条回答 默认 最新

  • dqybeh2884 2013-07-10 13:42
    关注

    PHP is evaluating $key['val'] as $key[0] and giving you the first letter of each "key"

    In your example, $key is not an array, it is a string containing computer, notebook or gadget for the respective iteration through $args. PHP evaluates 'val' as an integer because you are calling the array key syntax on a string.

    From the PHP Documentation

    Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. The functions substr() and substr_replace() can be used when you want to extract or replace more than 1 character.

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办