donglu953744 2016-03-22 04:44
浏览 7

数组中的两种方式有什么不同?

<?php
//I have an array like this
$ar = array('0', '1', '2', '3', '4', '5');

for($i = 0; $i < count($ar); $i++)
{
echo $ar[$i+1]; //way1
echo $ar[$i] +=1 //way2
}

?>

So is it different between way1 and way 2 because i saw the same result ???

  • 写回答

3条回答 默认 最新

  • doujiao9866 2016-03-22 04:49
    关注

    Please clarify what exactly you're looking for here.

    These two are totally different approaches and give you different results when your array values are something else.

    echo $ar[$i+1];  // Means you're printing the values corresponding to array keys 1, 2, 3.....
    
    echo $ar[$i] +=1 // Means you're getting the values of keys 0, 1, 2... and adding 1 to each of the values.
    

    If you have an array $ar = array(3, 6, 2, 10), these two will work this way:

    echo $ar[$i+1]; // 6, 2, 10 ....
    
    echo $ar[$i] +=1 // 4, 7, 3, 11....
    

    Hope this helps.

    Peace! xD

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入