Reputation: 51
i'm creating an PHP Function and i get one problem...
I can't set the foreach value into a variable inside the function.
My code is this...
The array:
$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
)
)
);
And the function with foreach's
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;
}
Okay, now i'm trying this one...
echo obtener_precio(1, 200);
And from it' i want to get result ===> 54.2, but i always got 0
Where is the problem?, Thanks!
Upvotes: 1
Views: 2095
Reputation: 16307
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);
Upvotes: 0
Reputation: 32750
$baremos_precios
is not defined in side the function
Use :
function obtener_precio($baremos_precios, $baremo, $kg){
And to call it like:
echo obtener_precio($baremos_precios, 1, 200);
===========================================================
Instead of this :
$kilos_array = [$secondkey];
if($kilos_array[0] == $kg){
$precio = $secondvalue;
}
use :
if($secondkey == $kg){
$precio = $secondvalue;
}
Better i will suggest you to change the complete foreach here :
foreach ($value['kilos_precio'] as $secondkey => $secondvalue) {
$kilos_array = [$secondkey];
if($kilos_array[0] == $kg){
$precio = $secondvalue;
}
}
to :
if(array_key_exists($kg,$value['kilos_precio'])){
$precio = $value['kilos_precio'][$kg];
}
Upvotes: 0
Reputation: 19111
if $baremos_precios
is a global variable do this:
function obtener_precio($baremo, $kg){
global $baremos_precios;
...
Upvotes: 0
Reputation: 173612
That's because you forgot to pass $baremos_precios
into the function itself, without which you can't reach it from within the function body (unless you use dirty global
tricks). So you should have this:
function obtener_precio($baremos_precios, $baremo, $kg){
And to call:
echo obtener_precio($baremos_precios, 1, 200);
Upvotes: 2