duandongji2231 2016-10-21 18:10
浏览 48

乘以3维数组php

This is a homework question. I'm not asking for the full answer just some tips to keep moving along. I've been working on this for way to long. I'm multiplying the 3 dimensional array contains a list of people and the qty of tools they are buying I'm multiplying the item qty by an array of prices. The problem I'm having is that all three arrays are giving the same total instead of multipying each by a different factor. I've tried a few different approaches. I figured I need to use a while statement or if else to multiply each price to it's $key=>$value, but can't seem to get it to work.

Homework Question

  <?php
               $names = array(array( 'Name' => 'Tom',
                                     'Hammers' => 3,
                                     'Saws' => 2,
                                     'Drills' => 2
                                    ),
                       array( 'Name' => 'Sue',
                                     'Hammers' => 2,
                                     'Saws' => 3,
                                     'Drills' => 2
                             ),
                       array( 'Name' => 'Steve',
                              'Hammers' => 2,
                              'Saws' => 2,
                              'Drills' =>2
                             )
                      );

               $prices = array('Hammers'=>3.00,'Saws'=>2.50,'Drills'=>3.25);

               for($array=0;$array<count($names);$array++){
                    foreach($names[$array] as $key=>$value)
                    {

                        $total= $names[0]["Hammers"] * $prices['Hammers'];
                        $total= $names[1]["Hammers"] * $prices['Hammers'];
                        $total= $names[2]["Hammers"] * $prices['Hammers'];
                        echo $key." = ".$value. "total".$total.'<br>';

                     }

                   }


              ?>       
  • 写回答

4条回答 默认 最新

  • duangu9173 2016-10-21 18:14
    关注

    The problem is here

    $total= $names[0]["Hammers"] * $prices['Hammers'];
    $total= $names[1]["Hammers"] * $prices['Hammers'];
    $total= $names[2]["Hammers"] * $prices['Hammers'];
    echo $key." = ".$value. "total".$total.'<br>';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作