0

我很难创建关联的数组并将值分配给键。我有两个数组(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); 
    }
4

3 回答 3

1
$tech_per_scan[$id] = $pay_per_scan; 
于 2013-05-27T09:18:43.083 回答
1

此行$tech_per_scan[] = array('id'=>$key,'pay_per_scan'=>$pay_per_scan);将向您的数组添加一个元素,它将以 0 作为其索引开始,因为您没有指定它的键。类似于array_push

它应该是$tech_per_scan[$id]

于 2013-05-27T09:20:10.903 回答
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 ; 
    }
于 2013-05-27T09:22:12.587 回答