fork download
  1. <?php
  2.  
  3. $numeros = array(1, 2, 3, -1, 4, -2, 5, -100, -5, 245, 10, 67, -0.23, 76, -100.23);
  4.  
  5. $negativos = preg_grep("/^\-\d*\.?\d+$/", $numeros);
  6.  
  7. print_r($negativos);
  8.  
  9. echo 'El array cuenta con ' . count($negativos) . ' nĂºmeros negativos';
  10.  
  11. ?>
Success #stdin #stdout 0.01s 82944KB
stdin
Standard input is empty
stdout
Array
(
    [3] => -1
    [5] => -2
    [7] => -100
    [8] => -5
    [12] => -0.23
    [14] => -100.23
)
El array cuenta con 6 números negativos