Мне нужен массив для экзамена, но результат после использования regex и foreach не тот, что я хочу.
Мой текущий результат:
Array
(
[0] => Array
(
[0] => 1 Q 1.Question 1? A 0 - Answer 11
[1] => 1 Q 1.Question 1? A 1 - Answer 12
[2] => 1 Q 1.Question 1? A 2 - Answer 13
[3] => 1 Q 1.Question 1? A 3 - Answer 21
[4] => 1 Q 1.Question 1? A 4 - Answer 22
[5] => 1 Q 1.Question 1? A 5 - Answer 31
[6] => 1 Q 1.Question 1? A 6 - Answer 32
[7] => 1 Q 1.Question 1? A 7 - Answer 33
[8] => 2 Q 2.Question 2? A 0 - Answer 11
[9] => 2 Q 2.Question 2? A 1 - Answer 12
[10] => 2 Q 2.Question 2? A 2 - Answer 13
[11] => 2 Q 2.Question 2? A 3 - Answer 21
[12] => 2 Q 2.Question 2? A 4 - Answer 22
[13] => 2 Q 2.Question 2? A 5 - Answer 31
[14] => 2 Q 2.Question 2? A 6 - Answer 32
[15] => 2 Q 2.Question 2? A 7 - Answer 33
[16] => 3 Q 3. Question 3? A 0 - Answer 11
[17] => 3 Q 3. Question 3? A 1 - Answer 12
[18] => 3 Q 3. Question 3? A 2 - Answer 13
[19] => 3 Q 3. Question 3? A 3 - Answer 21
[20] => 3 Q 3. Question 3? A 4 - Answer 22
[21] => 3 Q 3. Question 3? A 5 - Answer 31
[22] => 3 Q 3. Question 3? A 6 - Answer 32
[23] => 3 Q 3. Question 3? A 7 - Answer 33
)
)
В приведенном выше результате указано, сколько раз вопрос и ответ повторяются несколько раз.
Мой желаемый результат:
Array
(
[0] => Array
(
[0] => 1 Q 1.Question 1? A 0 - Answer 11
[1] => 1 Q 1.Question 1? A 1 - Answer 12
[2] => 1 Q 1.Question 1? A 2 - Answer 13
)
[1] => Array
(
[3] => 2 Q 2.Question 2? A 0 - Answer 21
[4] => 2 Q 2.Question 2? A 1 - Answer 22
)
[2] => Array
(
[5] => 3 Q 3.Question 3? A 0 - Answer 31
[6] => 3 Q 3.Question 3? A 1 - Answer 32
[7] => 3 Q 3.Question 3? A 2 - Answer 33
)
)
Мой текущий код (работает онлайн):
<?php
$re_question = "'<b>(.*?)</b>'si";
$re_answer = '#<input[^>]* value = "([a-z]\.\)(.*?)")>#s';
$str = '<b>1.Question 1?</b><div class = "data-check" style = "padding-left:25px;"><input class = "data-form-input" type = "radio" name = "q11111" value = "a.)Answer 11">a.)Answer 11</div><div class = "data-check" style = "padding-left:25px;"><input class = "data-form-input" type = "radio" name = "q11111" value = "b.)Answer 12">b.)Answer 12</div><div class = "data-check" style = "padding-left:25px;"><input class = "data-form-input" type = "radio" name = "q11111" value = "b.)Answer 13">b.)Answer 13</div><br><b>2.Question 2?</b><div class = "data-check" style = "padding-left:25px;"><input class = "data-form-input" type = "radio" name = "q11111" value = "a.)Answer 21">a.)Answer 21</div><div class = "data-check" style = "padding-left:25px;"><input class = "data-form-input" type = "radio" name = "q22" value = "b.)Answer 22">b.)Answer22</div><br><b>3. Question 3?</b><div class = "data-check" style = "padding-left: 25px;"> <input class = "data-form-input" type = "radio" name = "q33333" value = "a.) Answer 31"> a.) Answer 31 </div><div class = "data-check" style = "padding-left: 25px;"> <input class = "data-form-input" type = "radio" name = "q33333" value = "b.) Answer 32"> b.) Answer 32 </div> </div><div class = "data-check" style = "padding-left: 25px;"> <input class = "data-form-input" type = "radio" name = "q33333" value = "b.) Answer 33"> b.) Answer 33 </div>';
$questions = "";
$answers = [];
$questions_and_answers = [];
$answer_number = 0;
$question_number = 0;
//question
preg_match_all($re_question, $str, $matches_question, PREG_SET_ORDER, 0);
//if (count($matches_question) > 0) {
foreach($matches_question as $sentence_question){
$answer_number = 0;
$each_question = $sentence_question[1];
$question_number +=1;
preg_match_all($re_answer, $str, $matches_answer, PREG_SET_ORDER, 0);
foreach($matches_answer as $sentence_answer){
$questions_and_answers[] = $question_number. ' Q ' .$each_question. ' A ' .$answer_number.' - '.$sentence_answer[2];
$answer_number +=1;
}
//$result = array_merge($questions);
$result = array_merge([$questions_and_answers]);
}
print_r($result);
?>
Не могли бы вы помочь мне, как это решить?
Используйте 3v4l.org/5Ynp0 в качестве отправной точки. Это относится к предыдущему сообщению.
@ user3783243 спасибо за помощь. в результате не отображать значение. i.imgur.com/9zWVcYa.png Пожалуйста, проверьте, спасибо
Да, это отправная точка. Вы должны быть в состоянии построить из этого. У него есть точки данных, которые вам нужны.
Обратите внимание, что ответы вложены в каждый вопрос - не только foreach должен быть вложен, но и основное регулярное выражение. Попробуй это
<?php
//$str = ...
// $re_question_and_answers = '@\<b\>\d+\.\s*(?P<QUESTION>.*\?)\</b\>(?P<ANSWERS>(?:\<div[^\>]*\>\s*\<input[^\>]* value\=\"[^\"\)]+\)\s*[^\"]+\"\>(?:[^\<\>]*\</div\>)+' /* note: double " </div>" after Answer 32 */ .')+)(\<br\>|$)@iU'; // or '...@simU' // GOOD! but long and inflexible/immutable for "<div>" and "</div>"
$re_question_and_answers = '@\<b\>\d+\.\s*(?P<QUESTION>.*\?)\</b\>'
.'(?P<ANSWERS>'
.'(?:' // (start of 1 ANSWER)
.'([^\<\>]*|\</?(?!b\>)[^/\>]*\>)*' /* not "<tags>", "</tags>", or tags but not "<b>", "</b>" */
.'\<input[^\>]* value\=\"[^\"\)]+\)\s*[^\"]+\"\s*\>'
.'([^\<\>]*|\</?(?!b\>)[^/\>]*\>)*' /* not "<tags>", "</tags>", or tags but not "<b>", "</b>" */
.')+' // (end of 1 ANSWER)
.')' // end of 'ANSWERS'
.'(\<br\>|$)@iU';
$re_answer = '#<input[^>]* value = "([a-z]\.\)(?P<ANSWER>.*?)")>#s';
$questions = "";
$answers = [];
$questions_and_answers = [];
$answer_number = 0;
$question_number = 0;
preg_match_all($re_question_and_answers, $str, $matches_question_and_answers, PREG_SET_ORDER, 0);
//print_r($matches_question_and_answers);
$key_number = 0;
foreach($matches_question_and_answers as $sentence_question){
$each_question = $sentence_question['QUESTION'];
$question_number ++;
$answer_number = 0;
preg_match_all($re_answer, $sentence_question['ANSWERS'], $matches_answer, PREG_SET_ORDER, 0);
//print_r($matches_answer);
foreach($matches_answer as $sentence_answer){
$questions_and_answers[$question_number][$key_number] = $question_number. " Q $question_number." .$each_question. ' A ' .$answer_number.' - '.$sentence_answer['ANSWER'];
$answer_number ++;
$key_number ++;
}
}
echo 'Result: ';
print_r($questions_and_answers);
?>
Посмотрите https://onlinephp.io/c/76493
Это очень похоже на ваш последний вопрос, только с большим количеством примеров.