【问题标题】:how to pass array index value in dynamically如何动态传递数组索引值
【发布时间】:2013-09-02 06:16:32
【问题描述】:

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

在这里我正在自定义我的编码中使用的 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 的数组索引值。

在该编码中,我将从数据库中获取的值自定义为问题和答案。这里我不知道如何动态传递一个ItemCorrect值:

<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() 动态选择答案是对还是错?

【问题讨论】:

标签: php jquery ajax arrays codeigniter


【解决方案1】:

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

不要这样做,而是使用关联的 php 数组映射问题=>答案。然后你可以洗牌并保持秩序。

【讨论】:

  • 我怎么能在这里使用关联 php 数组映射,因为我在 $article->question and answers 中得到问题作为 $article->answers 获取整个数组值。在这我怎么能使用关联数组
猜你喜欢
  • 2015-11-25
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 2021-11-25
  • 1970-01-01
  • 2021-01-21
  • 2019-11-24
相关资源
最近更新 更多