我正在尝试迭代我的多维数组,以获取每个嵌套数组的选定值的索引。例如,当我运行循环时,我正在比较$ income,看它是否在每个数组的'范围之间。当'范围'发现我想把这个范围花费$ x并用它来制作基于' MinTax'和'比率'对于每个嵌套数组。到目前为止,我能够迭代$ TAX_RATES数组,但不知道如何存储每个嵌套数组的索引。不知道到目前为止该做什么。这是代码。
<?php
$income = filter_input(INPUT_POST, 'income');
define('TAX_RATES',
array(
'Single' => array(
'Rates' => array(10,15,25,28,33,35,39.6),
'Ranges' =>array(0,9275,37650,91150,190150,413350,415050),
'MinTax'=>array(0,927.50,5183.75,18558.75,46278.75,119934.75,120529.75),
),
'Married_Jointly' =>array(
'Rates' => array(10,15,25,28,33,35,39.6),
'Ranges' =>array(0,18550,75300,151900,231450,413350,466950),
'MinTax' =>array(0,1855.00,10367.50,29517.50,51791.50,111818.50,130578.50),
),
'Married_Seperately' =>array(
'Rates' => array(10,15,25,28,33,35,39.6),
'Ranges' =>array(0,9275,37650,75950,115725,206675,233475),
'MinTax'=>array(0,927.50,5183.75,14758.75,25895.75,5599.25,65289.25),
),
'Head_Household' =>array(
'Rates' => array(10,15,25,28,33,35,39.6),
'Ranges' =>array(0,13250,50400,130150,210800,413350,441000),
'MinTax' => array(0,1325.00,6897.50,26835.00,49417,116258.50,125936)
)
)
);
function incomeTax($income){
foreach(TAX_RATES as $lvl => $lvl_data){
$x=0;
while ($x <=7){
if($income>=TAX_RATES[$lvl]['Ranges'][$x]
&& $income<=TAX_RATES[$lvl]['Ranges'][$x+1]){
return $income=TAX_RATES[$lvl]['MinTax'][$x]
+((TAX_RATES[$lvl]['Rates'][$x]*.01)
*($income-(TAX_RATES[$lvl]['Ranges'][$x])));
}
$x++;
}
}
};
$results = incomeTax($income);
?>
答案 0 :(得分:0)
由于这个论坛,我能够弄明白。 https://www.codecademy.com/en/forum_questions/556d9d0ad3292f03fb000558。这是我的新代码。
function incomeTax($income){
foreach(TAX_RATES as $lvl => $lvl_data){
for($i=0; $i<count(TAX_RATES[$lvl]['Ranges']); $i++){
for($l=0; $l<count(TAX_RATES[$lvl]['Ranges'][$i]); $l++){
if($income>=TAX_RATES[$lvl]['Ranges'][$i]
&& $income<=TAX_RATES[$lvl]['Ranges'][$i+1])
{
echo $i;
}
}
}
}
};