在PHP中分配数组数据

时间:2017-01-14 11:53:48

标签: php

我确实搜索了这个,但可能是我的搜索很糟糕。所以在这里发布..我的代码在下面

while( $row = mysqli_fetch_array($queryRecords) ) {
            $data['data'][] = array(
                        'student_name' => $row['name'],
                        'totalmark'    => $row['totalmark'],
                         $marksheet    =  calculatepercent(totalmark),
                         'resultdate'  => $row['resultdate'].$marksheet,
                         'ID'          => $row['ID']
                         );

这里我调用函数calculatepercent(totalmark),以便函数返回一个值并将其存储在$ marksheet中。但我的问题是这不起作用,我无法存储calculatepercent(totalmark)的结果,因为我无法访问'totalmark'

忘记表演一分钟,但你如何使这项工作? (如果你有性能提示,这也是一个奖励!) - 谢谢编码!

RR

2 个答案:

答案 0 :(得分:1)

尝试这样。将$row['totalmark']发送到calculatepercent()函数。

while( $row = mysqli_fetch_array($queryRecords) ) {
            $data['data'][] = array(
                        'student_name' => $row['name'],
                        'totalmark'    => $row['totalmark'],
                         $marksheet    => calculatepercent($row['totalmark']),
                         'resultdate'  => $row['resultdate'].$marksheet,
                         'ID'          => $row['ID']
                         );

答案 1 :(得分:0)

我想我比其他任何人都更渴望得到答案!因此,在尝试了许多事情后,我让它工作并在这里为任何其他人分享

while( $row = mysqli_fetch_array($queryRecords) ) {
   $marksheet = calculatepercent($row['totalmark'];


            $data['data'][] = array(
                        'student_name' => $row['name'],
                        'totalmark'    => $row['totalmark'],
          //move the fn 'calculatepercent' outside while loop  $marksheet  => //calculatepercent($row['totalmark']),
                         'resultdate'  => $row['resultdate'].$marksheet,
                         'ID'          => $row['ID']
                         );