数组从零开始 [英] Array starting at Zero

查看:57
本文介绍了数组从零开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这行不通?我需要它从1而不是0开始。如果我将$ i更改为= 1,则它不会抓住第一行。

http://www.mcregister.com/beta/test.php

Why doesnt this work? I need it to start out at 1 instead of 0. And if I change $i to = 1 then it doesnt grab the first row.
http://www.mcregister.com/beta/test.php

<?php  
if(isset($_POST['question'])) {  
   for ($i=0; $i<count($_POST['question']);$i++) {  
      $question=$_POST['question'][$i]."<br />"; echo "<b>Question $i:</b> $question";  
   }    
}  
?>

编辑:而不是从1开始。我只需要它以问题1开头回显:,而不是问题0:。

Instead of starting out at 1.. I just need it to echo starting with "Question 1:" instead of "Question 0:".

推荐答案

似乎您想获取输出

<b>Question 1:</b> Blah Blah

但是。数组键默认情况下从0开始

But. The array key is start from 0 by default

有两种获取方法。如果您真的想要一个以键号1开头的数组。您可以通过以下代码来完成。

There have 2 way to get it. If you really want an array start with key number 1 . you can do by following code.

$new_array = array();
for ($i=0; $i<count($_POST['question']);$i++) {  
      $new_array[$i+1] = $_POST['question'][$i];  
} 

但是如果您只想拥有1.,可以这样做

but if you want to have just 1. you can do like

if(isset($_POST['question'])) {  
   for ($i=0; $i<count($_POST['question']);$i++) {  
      $question=$_POST['question'][$i]."<br />"; echo "<b>Question ".$i+1.":</b> $question";  
   }    
}

希望它会有所帮助。

这篇关于数组从零开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆