1

我正在创建一个 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

问题出在哪里,谢谢!

4

4 回答 4

2

那是因为你忘记了传递$baremos_precios给函数本身,没有它你不能从函数体内访问它(除非你使用肮脏的global技巧)。所以你应该有这个:

function obtener_precio($baremos_precios, $baremo, $kg){

并致电:

echo obtener_precio($baremos_precios, 1, 200);
于 2013-03-06T13:11:13.357 回答
0
    You can pass array as argument

function obtener_precio($baremos_precios,$baremo, $kg){
    //global $baremos_precios;
    $precio = 0;
    foreach($baremos_precios AS $key => $value){
        if($key == $baremo) {

            foreach ($value['kilos_precio'] as $secondkey => $secondvalue) {

                if($secondkey  == $kg){
                    $precio = $secondvalue;
                }
            }
        }
    }
    return $precio;
}
echo obtener_precio($baremos_precios,1, 200);

or you can use global variable 
function obtener_precio($baremo, $kg){
    global $baremos_precios;
    $precio = 0;
    foreach($baremos_precios AS $key => $value){
        if($key == $baremo) {

            foreach ($value['kilos_precio'] as $secondkey => $secondvalue) {

                if($secondkey  == $kg){
                    $precio = $secondvalue;
                }
            }
        }
    }
    return $precio;
}
echo obtener_precio(1, 200);
于 2013-03-06T13:34:39.340 回答
0

$baremos_precios未在函数中定义

利用 :

function obtener_precio($baremos_precios, $baremo, $kg){

并称之为:

echo obtener_precio($baremos_precios, 1, 200);

==================================================== =========

而不是这个:

$kilos_array = [$secondkey];
if($kilos_array[0] == $kg){
   $precio = $secondvalue;
}

利用 :

if($secondkey == $kg){
   $precio = $secondvalue;
}

更好的是,我建议您在这里更改完整的 foreach :

foreach ($value['kilos_precio'] as $secondkey => $secondvalue) {
    $kilos_array = [$secondkey];
    if($kilos_array[0] == $kg){
        $precio = $secondvalue;
    }
}

至 :

if(array_key_exists($kg,$value['kilos_precio'])){
    $precio = $value['kilos_precio'][$kg];
}
于 2013-03-06T13:11:09.673 回答
0

如果$baremos_precios是全局变量,请执行以下操作:

function obtener_precio($baremo, $kg){
  global $baremos_precios;
...
于 2013-03-06T13:11:53.527 回答