php - Filter string from inside an array -


i tried this:-

function sum_array($arr_sum){            $string= "";          $length= count($arr_sum);         $sum= null;         if(is_string($arr_sum)){                 echo "you cannot use string";             }else{                       for($i=0; $i<$length; $i++){                 $sum = $sum + $arr_sum[$i];             }                 echo " sum ". $sum;                 }     }     $array_summation=["string",12.6,25.2,10,12];     sum_array($array_summation); 

i want know should if want integer or float value inside array, , if string come inside array gives error no string wanted or

try this

function sum_array($arr_sum){            $sum = 0;         foreach($arr_sum $value){             if(!is_numeric($value)){                 echo 'array contain non numeric data';                 exit;             }             $sum += $value;         }         echo 'the sum '.$sum;     }      $array_summation=["string",12.6,25.2,10,12];     sum_array($array_summation); 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -