打印三维数组值

时间:2013-09-02 20:08:09

标签: php

我有一个数组,它有三个从db获取的值。我没有得到每个价值的印刷品。请帮我。

$sort_query = "SELECT s_uniqid, mar_tot, mar_per FROM  `exam_record` WHERE  bat=`1314`"
$run_sort = mysql_query($sort_query);

while($selectedvalue = mysql_fetch_assoc($run_sort))
{   

$total[] = $selectedvalue;
}
Print_r($total);

以上查询将结果显示为

Array ( [0] => Array ( [s_uniqid] => 1000017 [mar_tot] => 307 [mar_per] => 61.4 ) [1] => Array ( [s_uniqid] => 1000016 [mar_tot] => 288 [mar_per] => 57.6 ) [2] => Array ( [s_uniqid] => 1000015 [mar_tot] => 279 [mar_per] => 55.8 ) [3] => Array ( [s_uniqid] => 1000009 [mar_tot] => 269 [mar_per] => 53.8 ) [4] => Array ( [s_uniqid] => 1000010 [mar_tot] => 269 [mar_per] => 53.8 )

我想打印s_uniqid,mar_tot&的个别值。 mar_per ...请建议我..

3 个答案:

答案 0 :(得分:0)

这应该有帮助

foreach($total as $elem) {
    foreach($elem as $key => $val) {
        if($key == 's_uniqid')
            echo 'Unique ID: ' . $val;
        else if($key == 'mar_tot'
            echo 'Total Marks: ' . $val;
    }
}

答案 1 :(得分:0)

这样的事情可能会成功:

$s_uniqids, $mar_tots, $mar_pers;
foreach($run_sort as $part) {
  $s_uniqids = $part['s_uniqid'];
  echo "s_uniqid: " .$part['s_uniqid'];

  $mar_tots = $part['mar_tot'];
  echo $mar_tot " .$part['mar_tot'];

  $mar_pers = $part['mar_per'];
  echo "mar_per: " .$part['mar_per'];
}

$s_uniqids$mar_tots$mar_pers现在是带有提取值的数组。您可以使用我之前使用的foreach循环遍历各个值。

答案 2 :(得分:0)

警告,使用MYSQLI! MYSQL已弃用!!!

$sort_query = "SELECT s_uniqid, mar_tot, mar_per FROM  `exam_record` WHERE  bat=`1314`"
$run_sort = mysqli_query($sort_query);

$suniquid = array();
while($selectedvalue = mysqli_fetch_assoc($run_sort))
{   
   $total[] = $selectedvalue;
   $suniquid[] = $selectedvalue['s_uniqid'];
}
print_r($total);

print_r($suniquid); //Prints values of s_uniqid

<强>编辑:

while($selectedvalue = mysqli_fetch_assoc($run_sort))
{ 
   echo $selectedvalue['s_uniqid'];
   echo $selectedvalue['mar_tot'];
   echo $selectedvalue['amr_per'];
}

例如,如果您有两条记录,则会产生:

s_uniqid
mar_tot
amr_per
s_uniqid
mar_tot
amr_per

否则你可以这样做:

while($selectedvalue = mysqli_fetch_assoc($run_sort))
{
   $unique[] = $selectedvalue['s_uniqid'];
   $martot[] = $selectedvalue['mar_tot'];
   $amrper[] = $selectedvalue['amr_per'];
}

foreach($unique as $val)
{
  echo $val;
}

foreach($martot as $val)
{
  echo $val;
}

foreach($amrper as $val)
{
  echo $val;
}

这将导致(例如再次假设您有两条记录):

s_uniqid
s_uniqid
mar_tot
mar_tot
amr_per
amr_per