我很难创建关联数组并为键指定值。我有两个数组(tech_pay和tech_scans),我正在使用它们的值进行简单的计算,我想创建一个名为tech_per_scan的新数组,但我一直在从0开始自动创建一个数组。
$tech_per_scan = array();
foreach($tech_pay as $key=>$value)
{
$pay_per_scan = $tech_pay[$key]['tot_pay']/$tech_scans[$key]['scans'];//calculate the payment per scan
$tech_per_scan[] = array('id'=>$key,'pay_per_scan'=>$pay_per_scan);
}
答案 0 :(得分:1)
$tech_per_scan[$id] = $pay_per_scan;
答案 1 :(得分:1)
此行$tech_per_scan[] = array('id'=>$key,'pay_per_scan'=>$pay_per_scan);
将向您的数组添加一个元素,它将以0作为索引开始,因为您没有指定其键。与array_push
应为$tech_per_scan[$id]
答案 2 :(得分:0)
你应该像这样设置新数组的值:
$tech_per_scan[$key] = $pay_per_scan ;
完整代码是:
$tech_per_scan = array();
foreach($tech_pay as $key=>$value)
{
$pay_per_scan = $tech_pay[$key]['tot_pay']/$tech_scans[$key]['scans'];//calculate the payment per scan
$tech_per_scan[$key] = $pay_per_scan ;
}