Replace value in PHP array and call it later

13

I have a query and it is as follows.

My goal is to replace the values of all fruits to increase the price by 5%:

// precios originales

$precios = array (

    "frutas" => array (

"manzanas" => 15,
"peras" => 5,
"naranjas" => 3,
),

"verduras" => array (
  "clave" => 15,
  "clave2" => 5,
  "clave3" => 3,
)

);

// Los precios de frutas suben 5%

foreach ($precios['frutas'] as $clave => $valor) {
  $nuevovalor = $valor*5/100+$valor;
  $valor = ceil($nuevovalor);
}

// los precios de las verduras suben un 3%
foreach ($precios['verduras'] as $clave => $valor) {
  $nuevovalor = $valor*3/100+$valor;
  $valor = ceil($nuevovalor);
}

My idea is to use the function array_replace but until here I come, nothing I did seems to work to replace the values with the new prices, this in case the new prices are well resolved with that function, your idea It will be welcome, greetings.

    
asked by Javier Sal 11.02.2016 в 02:28
source

3 answers

6

I prefer to modify the array on itself instead of using array_replace . However, I left you both solutions. Test on IDEONE

<?php

$precios = array (

    "frutas" => array (

"manzanas" => 15,
"peras" => 5,
"naranjas" => 3,
),

"verduras" => array (
  "clave" => 15,
  "clave2" => 5,
  "clave3" => 3,
)

);

var_dump($precios);

// Los precios de frutas suben 5%

$tmp = array();

foreach ($precios['frutas'] as $clave => $valor) {
  $tmp[$clave]=ceil($valor*(1+0.05));
//  $precios['frutas'][$clave]=ceil($valor*(1+0.05));
}



$precios=array_replace($precios,array('frutas'=>$tmp));

$tmp = array();
// los precios de las verduras suben un 3%
foreach ($precios['verduras'] as $clave => $valor) {
  $tmp[$clave]=ceil($valor*(1+0.03));
 // $precios['verduras'][$clave]=ceil($valor*(1+0.03));
}
$precios=array_replace($precios,array('verduras'=>$tmp));


var_dump($precios);

?>

comments:

= is the way to make assignments in the "traditional" sense: what is to the right of the = is assigned to what is on the left; while => works the opposite and is used in "for" cycles. On the other hand, when multiplying a value by a decimal value, you obtain the percentage, being 0.05 equivalent to 5% and 1 equivalent to 100%. when adding both values I have 105%, which is the same: an increase of 5%.

    
answered by 11.02.2016 / 04:26
source
7

You got very complicated. Instead of traversing it with a foreach , go through the array with for and you can have the index the fix to manipulate your data.

With foreach you can do it but you must declare an incremental variable to use it as an index. Also do not use the name of variables as indexes, because you can not traverse them dynamically:

// precios originales

    $frutas = Array();
    $frutas[] = Array('nombre' => 'manzana, 'precio' => 15 );
    $frutas[] = Array('nombre' => 'peras, 'precio' => 10 );
    $frutas[] = Array('nombre' => 'naranja, 'precio' => 25 );


    for($i = 0 ; i < sizeof($frutas); $i++)
    {
      $frutas[$i]['precio'] = $frutas[$i]['precio'] + (0.05 * $frutas[$i]['precio']);
    }
    
answered by 11.02.2016 в 02:42
3

You can take advantage of the benefits of PHP 5.5 and use array_map and a lambda . The code is simplified enough:

$precios = array (
    "frutas" => array (
        "manzanas" => 15,
        "peras" => 5,
        "naranjas" => 3,
    ),
    "verduras" => array (
        "clave" => 15,
        "clave2" => 5,
        "clave3" => 3,
    )
);
//funcion definida con lambda
$agrega_porcentaje = function($n) { return 1.05 * $n; };
//utilizamos array_map para aplicar la funcion de mapeo en cada array
array_map($agrega_porcentaje, $precios['frutas']);
array_map($agrega_porcentaje, $precios['verduras']);

Even the second part of the code can be simplified even more, in case you have another type of item with its price plus fruits and vegetables:

//funcion definida con lambda
$agrega_porcentaje = function($n) { return 1.05 * $n; };
//utilizamos array_map para aplicar la funcion de mapeo en cada array
foreach ($precios as $elemento) {
    //sabemos que cada $elemento es un arreglo
    array_map($agrega_porcentaje, $elemento);
}
    
answered by 11.02.2016 в 06:27