我正在创建一个 PHP 函数,但我遇到了一个问题......
我无法将 foreach 值设置为函数内的变量。
我的代码是这样的...
数组:
$baremos_precios = array(
/* [baremo (id)] => [kilos_precio] => [kilos_hasta($key) => precio($value)] */
1 => array('kilos_precio' => array(
/* kilos hasta => precio (sin IVA) */
5 => 6.6,
10 => 7.76,
20 => 11.03,
30 => 14.79,
40 => 17.13,
50 => 19.46,
60 => 21.79,
70 => 24.12,
80 => 26.46,
90 => 28.8,
100 => 31.14,
120 => 35.8,
140 => 40.47,
160 => 45.14,
180 => 49.83,
200 => 54.2,
225 => 56.4,
250 => 58.59,
275 => 63.77,
300 => 68.94,
325 => 74.1,
350 => 79.28,
375 => 84.45,
400 => 89.62,
425 => 94.79,
450 => 99.96,
475 => 105.12,
500 => 108.08,
550 => 110.07,
600 => 113.08,
650 => 117.02,
700 => 125.53,
750 => 134.03,
800 => 142.52,
850 => 151.02,
900 => 159.53,
950 => 168.02,
1000 => 176.53,
1001 => 0.16 // precio por cada kilo apartir de >1000, ex: 1100kg => 0.16 * 1100 = 176€ + IVA
)
)
);
和foreach的功能
function obtener_precio($baremo, $kg){
$precio = 0;
foreach($baremos_precios AS $key => $value){
if($key == $baremo) {
foreach ($value['kilos_precio'] as $secondkey => $secondvalue) {
$kilos_array = [$secondkey];
if($kilos_array[0] == $kg){
$precio = $secondvalue;
}
}
}
}
return $precio;
}
好的,我现在正在尝试这个...
echo obtener_precio(1, 200);
并从中'我想得到结果 ===> 54.2,但我总是得到 0
问题出在哪里,谢谢!