Javascript 在循环中显示数据,但不是循环所有数据,而是在每个数据中出现中断

Javascript 在循环中显示数据,但不是循环所有数据,而是在每个数据中出现中断,javascript,php,jquery,Javascript,Php,Jquery,我试图根据数组的长度显示一系列问题 我想一个接一个地显示我的问题和选择,而不是全部显示出来 我的数组是这样的: 数组的选择 [0] => Array ( [0] => Array ( [id] => 25 [question_id] => 7 [choice] => gas shortage ) [1] => Array ( [id] => 26

我试图根据数组的长度显示一系列问题

我想一个接一个地显示我的问题和选择,而不是全部显示出来

我的数组是这样的:

数组的选择

[0] => Array (
    [0] => Array (
        [id] => 25
        [question_id] => 7
        [choice] => gas shortage
        )
    [1] => Array (
        [id] => 26
        [question_id] => 7
        [choice] => panic buying
        )
    [2] => Array (
        [id] => 27
        [question_id] => 7
        [choice] => Hurricane Harvey
        )
    [3] => Array (
        [id] => 28
        [question_id] => 7
        [choice] => help from oil companies
        )
    )
[1] => Array (
    [0] => Array (
        [id] => 29
        [question_id] => 8
        [choice] => for people to purchase normally
        )
    [1] => Array (
        [id] => 30
        [question_id] => 8
        [choice] => for markets to increase their prices
        )
    [2] => Array (
        [id] => 31
        [question_id] => 8
        [choice] => for people to stock up while they can
        )
    [3] => Array (
        [id] => 32
        [question_id] => 8
        [choice] => for drivers to rush to refill their tanks
        )
    )
[2] => Array (
    [0] => Array (
        [id] => 33
        [question_id] => 9
        [choice] => to make everyone panic over gas shortages
        )
    [1] => Array (
        [id] => 34
        [question_id] => 9
        [choice] => to publish how Texans reacted to fuel scarcity
        )
    [2] => Array (
        [id] => 35
        [question_id] => 9
        [choice] => to announce the effects of Hurricane Harvey in Texas
        )
    [3] => Array (
        [id] => 36
        [question_id] => 9
        [choice] => to inform the public of the closing of several oil refineries
        )
    )

问题(二)阵列

[0] => Array (
    [id] => 7
    [question] => How do the travel abroad courses help students?
    )
[1] => Array (
    [id] => 8
    [question] => Who can participate in the travel abroad program?
    )
[2] => Array (
    [id] => 9
    [question] => Which statement would the author likely agree with?
    )

我想要这样的输出

1.What was the Murrays’ overall impression of Randy’s service?
   A. gas shortage
   B. panic buying
   C  hurricane harvey
   D  help from oil companies

     Submit -> this will be button

然后,在他们单击提交后,将转到阵列上的另一个问题

这是我目前拥有的代码

function display() {
  $.ajax({
      url: url +,
      type: "GET",
      dataType: "json",
      success: function(data) {
        console.log(data);

        for (var i = 0; i < data.choices.length; i++) {

            if (data.choices.length < 0) {
              
            }else{
              $('#display_question').text(data.choices[i].question);
              data.choices.splice(i, 1);
              break;
            }

        }
      },
      error: function(jqXHR, textStatus, errorThrown) {
          alert('Error adding / update data');
      }
  });
}

我只能显示问题,但我仍然不知道如何从该问题中获得所有选择。谁能帮帮我,我真的不知道该怎么办。非常感谢您的帮助。

这将解决您的问题:

$questions = [
0 => [
    'question' => "What was the Murrays overall impression of Randy's service?",
    'choice' => 'gas shortage',
    ],
1 => [
    'question' => "What was the Murrays overall impression of Randy's service?",
    'choice' => 'panic buying',
    ],
2 => [
    'question' => "What was the Murrays overall impression of Randy's service?",
    'choice' => 'Hurricane Harvey',
    ],
3 => [
    'question' => "What was the Murrays overall impression of Randy's service?",
    'choice' => 'Help from oil companies',
    ],
4 => [
    'question' => 'What did Randy do first?',
    'choice' => 'for people to purchase normally'
    ],
5 => [
    'question' => 'What did Randy do first?',
    'choice' => 'for markets to increase their prices'
    ],
6 => [
    'question' => 'What did Randy do first?',
    'choice' => 'for people to stock up while they can'
    ],
7 => [
    'question' => 'What did Randy do first?',
    'choice' => 'for drivers to rush to refill their tanks'
    ],
];

$prevQues = '';
$newQues = [];
foreach($questions as $question) {
    $quesText = $question['question'];
    $quesChoice = $question['choice'];
    if($prevQues !== $quesText) {
        $newQues[] = ['question' => $quesText, 'choice' => [$quesChoice]];
        $prevQues = $quesText;
    } else {
        $newQues[count($newQues)-1]['choice'][] = $quesChoice;
    }
}
现在,$newQues将具有正确组合的数据。每个问题一次,所有选项作为一个数组

****已编辑****

$newQuesArr = [];
foreach($question_array as $index => $question) {
    $newQuesArr[$index] = ['question' => $question['question']];
    foreach($choices_array[$index] as $choice) {
        $newQuesArr[$index]['choices'][] = $choice['choice']; // changed
    }
}

让我们看看
url
@riggsfully指向的PHP脚本文件中的代码怎么样?您好,先生,请再次检查。我已经更新了它。所以数组是由PHP代码返回的,对吗?我想OP意识到了这一点,问题是我该怎么做???是的,这是我尝试的方法,但我真的很难处理数组上的数据。你是从get_question_choices函数获得你发布的数组吗?是的,先生@Tushar,我已经在我的控制器中编辑了我的代码,我已经将问题和选择的获取分开,我只是不知道如何才能使我的数组变成那样。@Tushar hi先生,我再次编辑了我的控制器,我已经根据问题表的长度和问题id循环选择了。我只需要把两者结合起来,但我不知道怎么做,
$questions = [
0 => [
    'question' => "What was the Murrays overall impression of Randy's service?",
    'choice' => 'gas shortage',
    ],
1 => [
    'question' => "What was the Murrays overall impression of Randy's service?",
    'choice' => 'panic buying',
    ],
2 => [
    'question' => "What was the Murrays overall impression of Randy's service?",
    'choice' => 'Hurricane Harvey',
    ],
3 => [
    'question' => "What was the Murrays overall impression of Randy's service?",
    'choice' => 'Help from oil companies',
    ],
4 => [
    'question' => 'What did Randy do first?',
    'choice' => 'for people to purchase normally'
    ],
5 => [
    'question' => 'What did Randy do first?',
    'choice' => 'for markets to increase their prices'
    ],
6 => [
    'question' => 'What did Randy do first?',
    'choice' => 'for people to stock up while they can'
    ],
7 => [
    'question' => 'What did Randy do first?',
    'choice' => 'for drivers to rush to refill their tanks'
    ],
];

$prevQues = '';
$newQues = [];
foreach($questions as $question) {
    $quesText = $question['question'];
    $quesChoice = $question['choice'];
    if($prevQues !== $quesText) {
        $newQues[] = ['question' => $quesText, 'choice' => [$quesChoice]];
        $prevQues = $quesText;
    } else {
        $newQues[count($newQues)-1]['choice'][] = $quesChoice;
    }
}
$newQuesArr = [];
foreach($question_array as $index => $question) {
    $newQuesArr[$index] = ['question' => $question['question']];
    foreach($choices_array[$index] as $choice) {
        $newQuesArr[$index]['choices'][] = $choice['choice']; // changed
    }
}