多维数组Foreach源+ [英] Multidimensional Array + Foreach

查看:85
本文介绍了多维数组Foreach源+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建使用存储在数据库表中的值一些在线测验。每个页面将显示10个问题,每个问题后面四个可能的答案......

 什么颜色的苹果吗?

黄色
蓝色

我知道如何做一个简单的阵列将显示所有的问题。或者,我可以显示所有的答案。但分组每组答案与适当的答案是棘手。

有人建议我用一个多维数组用foreach相结合。于是我开始检查出一些相关的主题,其中包括一个@ <一个href=\"http://stackoverflow.com/questions/17729301/php-pdo-fetchall-while-not-working-foreach-works\">php PDO使用fetchall() - 而不是工作,工作的foreach 但我完全下雪

这是我想出迄今...

  $ =问题阵列('问题'=&GT; $问,'答案'=&GT;阵列($回答));的foreach($问题为$问题=&GT; $答案){
  回声$问题。'&LT; BR /&GT;';
 }

但它可能有更多的错误比我可以指望。这是至少在正确的轨道上?

我下面贴我的code。

  $语句= $ pdo-&GT; prepare(选择T.URL,TQ.URL,TQ.QID,TQ.Question,TQ.Feedback,TA.URL ,TA.QID QID2,TA.Value,TA.Answer,TA.Correct
从g_testsŧ
LEFT JOIN g_test_questions TQ ON TQ.URL = T.URL
LEFT JOIN g_test_answers TA ON TA.URL = T.URL
WHERE T.URL ='GW-介绍-1'AND TQ.QID = TA.QID
ORDER BY TA.N);
$ stmt-&GT;执行(阵列(
MyURL'=&GT; $ MyURL
));而($行= $ stmt-&GT;取()){
    $ URL = $行['URL'];
    $ QID = $行['QID'];
    $ QID2 = $行['QID2'];
    $问题= $行['问题'];
    $反馈= $行['反馈'];
    $值= $行['值'];
    $答案= $行['答案'];
    $正确= $行['正确'];
    $正确= str_replace函数('1','正确的',$正确的);
    $正确= str_replace函数('2','错',$正确的);    $问题=阵列('问题'=&GT; $问,'答案'=&GT;阵列($回答));    的foreach($问题为$问题=&GT; $答案){
        回声$问题。'&LT; BR /&GT;';
    }
}

我编辑这表明我修改code:

  $语句= $ pdo-&GT; prepare(选择T.Site,T.Type,T.URL,T.Section,T.URL_Foreign,T.Title ,T.Subtitle,T.Parent,T.Live,TQ.URL,TQ.QID,TQ.Question,TQ.Feedback,TA.URL,TA.QID QID2,TA.Value,TA.Answer,TA.Correct
从g_testsŧ
LEFT JOIN g_test_questions TQ ON TQ.URL = T.URL
LEFT JOIN g_test_answers TA ON TA.URL = T.URL
WHERE T.URL ='GW-介绍-1'AND TQ.QID = TA.QID
ORDER BY TA.N);$ stmt-&GT;执行(阵列('MyURL'=&GT; $ MyURL));$阵列= $ stmt-&GT;使用fetchall(PDO :: FETCH_ASSOC);的foreach($数组作为$数据集){
 的foreach($数据集作为$栏=&GT; $值){
    回声列[$列]包含[$值]&LT; BR&GT; \\ r \\ n;
 }
}


解决方案

您可以,或者,尝试 $ stmt-&GT;使用fetchall(PDO :: FETCH_ASSOC)。这并不直接给你的数据集的数组。的结构将如下:

 阵列(datasetNumber=&GT;
    阵列(
        Row1Name=&GT; Row1Value
        Row2Name=&GT; Row2Value
        ...
    )

您可以通过使用这种类型的数组循环

 的foreach($数组作为$数据集){
    的foreach($数据集作为$栏=&GT; $值){
        回声列[$列]包含[$值] \\ r \\ n;
    }
}

因此​​,你可以离开了。

 而($行= $ stmt-&GT;取()){
    $ URL = $行['URL'];
    $ QID = $行['QID'];
    $ QID2 = $行['QID2'];
    $问题= $行['问题'];
    $反馈= $行['反馈'];
    $值= $行['值'];
    $答案= $行['答案'];
    $正确= $行['正确'];
    $正确= str_replace函数('1','正确的',$正确的);
    $正确= str_replace函数('2','错',$正确的);    $问题=阵列('问题'=&GT; $问,'答案'=&GT;阵列($回答));    的foreach($问题为$问题=&GT; $答案){
         回声$问题。'&LT; BR /&GT;';
    }
 }

I want to create some online quizzes using values stored in a database table. Each page will display ten questions, with each question followed by four possible answers...

What color are apples?
red
yellow
blue
pink

I know how to make a simple array that will display all the questions. Or I can display all the answers. But grouping each set of answers with the appropriate answer is trickier.

Someone suggested I use a multidimensional array combined with foreach. So I started checking out some related threads, including the one @ php PDO fetchAll() - while not working, foreach works but I'm completely snowed.

This is what I've come up with so far...

$Questions = array('question' => $Question, 'answers' => array($Answers));

foreach ($Questions as $Question => $Answers) {
  echo $Questions.'<br />';
 }

But it probably has more errors than I can count. Is this at least on the right track?

I posted my code below.

$stmt = $pdo->prepare("SELECT T.URL, TQ.URL, TQ.QID, TQ.Question, TQ.Feedback, TA.URL, TA.QID QID2, TA.Value, TA.Answer, TA.Correct
FROM g_tests T
LEFT JOIN g_test_questions TQ ON TQ.URL = T.URL
LEFT JOIN g_test_answers TA ON TA.URL = T.URL
WHERE T.URL = 'gw-intro-1' AND TQ.QID = TA.QID
ORDER BY TA.N");
$stmt->execute(array(
'MyURL'=>$MyURL
));

while ($row = $stmt->fetch()) {
    $URL = $row['URL'];
    $QID = $row['QID'];
    $QID2 = $row['QID2'];
    $Question = $row['Question'];
    $Feedback = $row['Feedback'];
    $Value = $row['Value'];
    $Answer = $row['Answer'];
    $Correct = $row['Correct'];
    $Correct = str_replace('1', 'correct', $Correct);
    $Correct = str_replace('2', 'wrong', $Correct);

    $Questions = array('question' => $Question, 'answers' => array($Answers));

    foreach ($Questions as $Question => $Answers) {
        echo $Questions.'<br />';
    }
}

I edited this to show my revised code:

$stmt = $pdo->prepare("SELECT T.Site, T.Type, T.URL, T.Section, T.URL_Foreign, T.Title, T.Subtitle, T.Parent, T.Live, TQ.URL, TQ.QID, TQ.Question, TQ.Feedback, TA.URL, TA.QID QID2, TA.Value, TA.Answer, TA.Correct
FROM g_tests T
LEFT JOIN g_test_questions TQ ON TQ.URL = T.URL
LEFT JOIN g_test_answers TA ON TA.URL = T.URL
WHERE T.URL = 'gw-intro-1' AND TQ.QID = TA.QID
ORDER BY TA.N");

$stmt->execute(array('MyURL' => $MyURL));

$Array = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($Array as $dataset){
 foreach ($dataset as $Column => $Value){
    echo "The column [$Column] contains [$Value] <br> \r\n";
 }
}

解决方案

You could, alternatively, try $stmt->fetchAll(PDO::FETCH_ASSOC). That does directly give you an array of datasets. The structure will be as follows :

Array ( "datasetNumber" =>
    Array (
        "Row1Name" => "Row1Value",
        "Row2Name" => "Row2Value",
        ...                 
    )
)

You may loop through this kind of array using

foreach ($Array as $dataset){
    foreach ($dataset as $Column => $Value){
        echo "The column [$Column] contains [$Value] \r\n";
    }
}

Thus you could leave out

while ($row = $stmt->fetch()) {
    $URL = $row['URL'];
    $QID = $row['QID'];
    $QID2 = $row['QID2'];
    $Question = $row['Question'];
    $Feedback = $row['Feedback'];
    $Value = $row['Value'];
    $Answer = $row['Answer'];
    $Correct = $row['Correct'];
    $Correct = str_replace('1', 'correct', $Correct);
    $Correct = str_replace('2', 'wrong', $Correct);

    $Questions = array('question' => $Question, 'answers' => array($Answers));

    foreach ($Questions as $Question => $Answers) {
         echo $Questions.'<br />';
    }
 }

这篇关于多维数组Foreach源+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆