【问题标题】:How to print according to number of objects in PHP如何根据PHP中的对象数量打印
【发布时间】:2012-10-06 04:01:14
【问题描述】:

我想根据这个模式打印对象的数量:

If there is 1 object, print 0
If there is 2 objects, print 0 1
If there is 3 objects, print 0 1 2

我尝试了以下代码:

for($i = count($nodes) ; $i >= 0 ; $i--){
print $i;
}

但结果是:

If there is 1 object, print 0 1
If there is 2 objects, print 0 1 2
if there is 3 objects, print 0 1 2 3

我不能使用。我怎样才能做到这一点?

【问题讨论】:

    标签: php arrays for-loop


    【解决方案1】:

    你应该这样使用:

     $n = 3; // where n is no. of object
    for($i=0; $i<$n; $i++){
        print $i." ";
        }
     //output 0 1 2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 2011-12-07
      • 2018-12-29
      相关资源
      最近更新 更多