如何动态传递数组索引值

时间:2013-09-02 06:16:32

标签: php jquery ajax arrays codeigniter

我想知道如何动态获取数组索引值。

这里我正在自定义编码中使用的quizy-fillintheblanks.master.zip文件:

<script>
  $('#tutorial-fillblank').quizyFillBlank({
    textItems:['The first president of the United States of America is', '. The longest serving president of the country is ', '. He was succeeded by ', ', who led the country till the end of the Second World War. The first afro-american to be elected for this position is', '.'],
    anItems:['John Kennedy', 'Franklin Roosevelt', 'George Washington', 'Ronald Reagan', 'Harry Truman', 'Richard Nixon', 'Barack Obama' ], 
    anItemsCorrect:[2,1,4,6],
    blockSize:150
  });
</script>

在上面的编码中,textItems是问题,anItems是答案,anItemCorrect是anItems的数组索引值。

在那个编码中,我正在定制我从数据库中获取的问题和答案的值。这里我不知道如何动态传递anItemCorrect值:

<script>
  $('#tutorial-fillblank').quizyFillBlank({
    textItems:[<?php foreach($question as $article)  { echo "'".$article->question."',";}?>],
    anItems:[<?php foreach($question as $article)
    {$a[]=$article->answer;}
    $arr=$a; shuffle($arr); foreach($arr as $ans => $val) {echo "'".$val."',";} ?> ], 
    anItemsCorrect:[<?php foreach($arr as $ans => $val) { echo "'".$ans."',";} ?>],
    blockSize:150
  });
</script>

如何使用jquery.inArray()动态选择答案是对还是错?

1 个答案:

答案 0 :(得分:0)

你不能,因为你洗了问题,所以你不知道答案的顺序。

而不是这样做,使用关联的php数组映射问题=&gt;回答。然后你可以随机播放,维持秩序。