美好的一天,我有一个这样的数组:
$cart = [
'id' => 1,
'item_name' => 'sample',
'quantity' => 20,
'price' => 50,
];
我试过这样做:
'total' => $cart['quantity'] * $cart['price']
我得到一个 undefined index 错误。
有什么办法可以实现吗
注意:'total'
键在同一个数组 $cart
中。
最佳答案
您不能访问尚未创建的索引
这样试试
$cart = [
'id' => 1,
'item_name' => 'sample',
'quantity' => 20,
'price' => 50,
];
$cart['total'] = $cart['quantity'] * $cart['price'];
关于php - 是否可以访问同一数组中的键 - PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42070776/