dowb58485 2014-12-19 14:19
浏览 20

我可以在阅读时操纵关联数组吗?

I'm bringing in this array for datatables and putting it into an associative array. Before I was using datatables I was using straight PHP to create my table and could manipulate the indexed arrray, here's an example

    <?php
    foreach ($types as $val) {
    setlocale(LC_MONETARY, 'en_US');
    echo '<tr>';
    echo '<td>' . $val[0] . '</td>';
    echo '<td>' . $val[1] . '</td>';
    echo '<td>' . $val[5] . '</td>';
    echo '<td>' . number_format($val[2] * .01, 2) . '</td>';
    echo '<td>' . $val[3] . '</td >';
    echo '<td>' . money_format('%2n', $val[4] * .01) . '</td>';



    switch ($val[5]) {
        case 'S';
            echo '<td>' . money_format('%2n', $val[3]) . '</td>';
            break;
        case 'U';
            echo '<td>U</td>';
            break;
        case 'H':
            $totalHourly = ($val[2] * .01) * ($val[4] * .01);
            echo '<td>' . $totalHourly . '</td>';
            break;
        case 'M':
            echo '<td>M</td>';
            break;
        default;
            if ($val[2] > 0) {
                $totalGross = ($val[2] * .01) * ($val[4] * .01);
                echo '<td>' . money_format('%.2n', $totalGross) . '</td>';
            } else {
                echo '<td>' . money_format('%.2n', $val[3]) . '</td>';
            }
    }

    echo '<td><a href=EmployeeInfo.php?empNum=' . $val[0] . '>Info</a></td>';
    echo '<td><a href=EmployeePayroll.php?empNum=' . $val[0] . '>Payroll</a></td>';
    echo '</tr>';
}
?>

Now I am using datatables and from the example am using an associative array. The reason I'm using datatables is for the hide and show chid row. Here is an example of my associative array.

while ($row = db2_fetch_array($stmt)) {

        $load['data'][] = array(
            'empNum' => $row[0],
            'empName' => $row[1],
            'unitRate' => $row[2],
            'salary' => $row[3],
            'hourly' => $row[4],
            'appFlag' => $row[5],
            'app1' => $row[6],
            'app2' => $row[7],
            'app3' => $row[8],
            'app4' => $row[9],
            'app5' => $row[10],
            'uni1' => $row[11],
            'uni2' => $row[12],
            'uni3' => $row[13],
            'uni4' => $row[14],
            'uni5' => $row[15],
            'gross' => ($row[2] * .01) * ($row[4] * .01)
        );
    }

The very last line is the simplest way I have found of manipulating this array. Is there a way I can apply the same logic to my associative array the same way I did my indexed array? And if not, what is the best way of adding logic and then creating an associative array?

This is what I did.

while ($row = db2_fetch_array($stmt)) {
            $empNum = $row[0];
            $empName = $row[1];
            $unitRate = $row[2];
            $salary = $row[3];
            $hourly = $row[4];
            $appFlag = $row[5];
            $app1 = $row[6];
            $app2 = $row[7];
            $app3 = $row[8];
            $app4 = $row[9];
            $app5 = $row[10];
            $uni1 = $row[11];
            $uni2 = $row[12];
            $uni3 = $row[13];
            $uni4 = $row[14];
            $uni5 = $row[15];

       switch ($row[5]) {
            case 'S';
                $gross =  $row[3];
                break;
            default;
                if ($row[2] > 0) {
                    $gross = ($row[2] * .01) * ($row[4] * .01);
                } else {
                    $gross = $row[3];
                }
        }

         $load['data'][] = array(
             'empNum' => $empNum,
             'empName' => $empName,
             'unitRate' => $unitRate,
             'salary' => $salary,
             'hourly' => $hourly,
             'appFlag' => $appFlag,
             'app1' => $app1,
             'app2' => $app2,
             'app3' => $app3,
             'app4' => $app4,
             'app5' => $app5,
             'uni1' => $uni1,
             'uni2' => $uni2,
             'uni3' => $uni3,
             'uni4' => $uni4,
             'uni5' => $uni5,
             'gross' => $gross
         );
  • 写回答

3条回答 默认 最新

  • douzhan4522 2014-12-19 14:32
    关注

    There's no reason to do it all in one shot. You can create your child array, manipulate it however you want, THEN stuff it into the parent array

    $foo = array()
    $foo['empnum'] = ...
    ...
    $foo['gross'] = $foo['x'] * $foo['y'];
    if ( ...) {
       $foo['if_results'] = ... :
    }    
    $data[] = $foo;
    

    Note that you cannot embed if/switch inside the ... = array(...) definition. if/switch are not function calls, they do not have return values, and therefore can be used in a context where they'd expected to be function calls.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?