简体   繁体   中英

PHP Array - some further problems with displaying elements in the array

Using Brian's suggestion below:

foreach($qs as $value){
                echo "<tr>".$value['qnum']." is the questions number and the question text is ".$value['qtext'].". The page and q values are ".$value['page']." and ".$value['questions']." respectively.</tr>";
        }

I get the following output from the array, which isn't right:

8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively

Any further suggestions.

Homer.

Hi again,

Thanks in advance for the help.

Here is my array based on $rowq :

Array (
 [0] => Array (
       [questions] => q8
       [qnum] => 8
       [qtext] => I know how what I do fits into my team's objectives
       [page] => 1
       )
 [1] => Array (
       [questions] => q8
       [qnum] => 8
       [qtext] => I know how what I do fits into my team's objectives
       [page] => 1
       ) 
 [2] => Array (
       [questions] => q8
       [qnum] => 8
       [qtext] => I know how what I do fits into my team's objectives
       [page] => 1
       ) 
 [3] => Array (
       [questions] => q8 
       [qnum] => 8
       [qtext] => I know how what I do fits into my team's objectives
       [page] => 1
       )
 [4] => Array (
       [questions] => q8
       [qnum] => 8
       [qtext] => I know how what I do fits into my team's objectives
       [page] => 1  
       )
 [5] => Array (
      [questions] => q8 
      [qnum] => 8 
      [qtext] => I know how what I do fits into my team's objectives
      [page] => 1 
      )
 [6] => Array (
      [questions] => q8 
      [qnum] => 8
      [qtext] => I know how what I do fits into my team's objectives
      [page] => 1
      )
 [7] => Array (
      [questions] => q8
      [qnum] => 8
      [qtext] => I know how what I do fits into my team's objectives
      [page] => 1
      ) 
 )

I would like to display elements of the array, looping through each row using (i think) the foreach statement but I can't get it working, ideally, I would like to echo something like this

echo "<tr>".$rowq[qnum]." is the questions number and the question text is ".$rowq[qtext].".  The page and q values are ".$rowq[page]." and ".$rowq[questions]." respectively.";

And that link of text would appear however many rows the array has in it.

Any and all advice appreciated - I'm struggling like heck to get my head around multi-dimensional arrays :(

Homer.

modifications from TriLLi

1) you don't need the $key since you never use it - in fact php will report a warning or notice

2) avoid using the error suppression operator, @, if you don't need to

3) also you wanted <tr> tags, right?

foreach($rowq as $value){
            echo "<tr><td>".$value['qnum']." is the questions number 
  and the question text is ".$value['qtext'].". The page and q values are".
$value['page']." and ".$value['questions']." respectively.</td></tr>";
    } 

使用以下代码foreach($rowq as $key => $value)
{
echo @"".$value['qnum']." is the questions number and the question text is ".$value['qtext'].". The page and q values are ".$value['page']." and ".$value['questions']." respectively.";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM