用虚拟值初始化数组

时间:2009-12-28 20:36:28

标签: php

 while ($row = mysql_fetch_object($result)) {
    $data[] = $row;
    echo "<div id='captionbox' style='width: 110px;float:left;color:#FFF;text-align:center;'>";
        echo "<a href='#' class='thumb'><img class='thumb-img' value = ".$row->aid." onclick='getVote(".$row->aid.", \"".$row->atitle."\")' src='images/roadies/th".$row->aid.".jpg' /> </a>";
    echo "<input  type = hidden name = aid id = rd".$row->aid." value = ".$row->aid.">".$row->atitle."</input>";
    echo "</div>";
    }

    $jsfriend = json_encode($data);

在上面的PHP代码中,我将mysql行添加到数组$ data中。然后我从该数组中创建一个JSON对象。在进入while循环之前,我想创建数组$ data并用虚拟值初始化$ data [0]指针,因为我不想使用[0]指针值。可以这样做吗?

我希望我有意义。

2 个答案:

答案 0 :(得分:0)

这样的东西?

$data=array("dummyfirst");
while(....

答案 1 :(得分:0)

在我看来,你需要做的就是在while循环之前分配一个值。

$data[0] = "Dummy Values"; // the 0 isn't really needed if this is a new array
while ($row = mysql_fetch_object($result)) {
  $data[] = $row; // first time through will be in $data[1]
}