Ver Mensaje Individual
  #4 (permalink)  
Antiguo 20/03/2013, 11:22
Avatar de informacionsys
informacionsys
 
Fecha de Ingreso: mayo-2011
Ubicación: Bogota D.C
Mensajes: 793
Antigüedad: 13 años
Puntos: 76
Respuesta: Como sumar solo variables positivas

prueba esto

Código PHP:
Ver original
  1. function is_negative($valor)
  2. {
  3.     if(is_int(strpos($valor,"-")))
  4.     {
  5.     return true;
  6.     }
  7.     else  
  8.     {
  9.     return false;
  10.     }
  11. }
  12.  
  13. $valores  = array("1","-20","45","-95");
  14.  
  15. foreach($valores as $v)
  16. {
  17.     $negativo = is_negative($v);
  18.  
  19.     if(!$negativo)
  20.     {
  21.         $sume += $v;
  22.     }
  23.  
  24. }
  25.  
  26. echo $sume;