如何访问类对象以显示PHP中的总价?

如何访问类对象以显示PHP中的总价?,php,Php,问题:analytics2.php还通过调用 getCustomerList()。 处理客户对象的索引数组,以识别花费最多的客户。对于 “bob”(如上面的内存状态图所示),他以每只5美元的价格购买了5个苹果(小计:25美元) 和两个香蕉,每个10美元(小计:20美元)。他总共花了45美元 下面是我试图编写的analytics2.php,但我不知道如何进一步获得每个客户的总价: require_once 'CustomerDAO.php'; $dao = new CustomerDAO();

问题:analytics2.php还通过调用 getCustomerList()。 处理客户对象的索引数组,以识别花费最多的客户。对于 “bob”(如上面的内存状态图所示),他以每只5美元的价格购买了5个苹果(小计:25美元) 和两个香蕉,每个10美元(小计:20美元)。他总共花了45美元

下面是我试图编写的analytics2.php,但我不知道如何进一步获得每个客户的总价:

require_once 'CustomerDAO.php';

$dao = new CustomerDAO();
$customer_list = $dao->getCustomerList();

// YOUR CODE GOES HERE
$subtotal = 0;
$total = 0;
$mostSpent = 0;
$customerList = [];

foreach($customer_list as $customerObj){
    
    $customerName = $customerObj->getName();
    $order_list = $customerObj->getOrderList();
    // var_dump($order_list);

    foreach($order_list as $orderObj){
        
        $qty = $orderObj->getQuantity();
        $price = $orderObj->getItem()->getPrice();
        

        if(sizeof($order_list) > 1){
            
            for($i = 0; $i < sizeof($order_list); $i++){
                $subtotalList = [];
                $subtotal = $qty * $price;
                $subtotalList[] = $subtotal;
            }
           
            
        }
        else{
            $subtotal = $qty * $price;

            
        }
       

        // var_dump($qty);
    }


    

}
echo "The customer who spent the most money is ... (... dollars)";

###
require_once'CustomerDAO.php';
$dao=新客户dao();
$customer_list=$dao->getCustomerList();
//你的密码在这里
$小计=0;
$total=0;
$mostSpent=0;
$customerList=[];
foreach($customer\u列表为$customerObj){
$customerName=$customerObj->getName();
$order_list=$customerObj->getOrderList();
//变量转储(订单列表);
foreach($order\u列表为$orderObj){
$qty=$ORDERBJ->getQuantity();
$price=$orderObj->getItem()->getPrice();
如果(sizeof($order_list)>1){
对于($i=0;$i
这是CustomerDAO.php:

<?php

// DO NOT MODIFY THE CODE BELOW

class CustomerDAO{

    private $customerList;

    # Constructor
    # Simulates retrieval from a database
    public function __construct(){
        
        # Items
        $apple = new Item("apple",5);
        $banana = new Item("banana",10);
        $orange = new Item("orange",15);

        # Orders
        $order1 = new Order($apple,5);
        $order2 = new Order($banana,2);
        $order3 = new Order($apple,10);
        $order4 = new Order($apple,1000);

        $orderList1 = [$order2, $order1];
        $orderList2 = [$order4];
        $orderList3 = [$order1, $order2, $order3];
        
        # Customers
        $bob = new Customer("bob", $orderList1);
        $jane = new Customer("jane", $orderList2);
        $jill = new Customer("jill", $orderList3); 

        $this->customerList = [$bob, $jane, $jill];
    }

    # Get a list of customers 
    # Input: Nothing
    # Output: A list of Customer objects
    public function getCustomerList(){
        return $this->customerList;
    }
}   ?>

预期输出应如下所示:

为什么在同一阵列上有两个嵌套循环
foreach($order_list
),然后
for($i=0;$i
?@Syscall,因为我正在尝试向下钻取类对象中的类对象以显示总计,到目前为止,我只能获得小计