2

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 " The Sum is ". $sum;
                }
    }
    $array_summation=["string",12.6,25.2,10,12];
    sum_array($array_summation);

I want to know what should i do if i want only integer or float value inside an array, and if string come inside array it gives error that no string wanted or something like that

1
  • You should accept an answer Commented Aug 24, 2016 at 4:54

3 Answers 3

1

Use array_map to get the type of each value present in the array -

$type = array_map('gettype', $array_summation);
if (!empty($type) && in_array('string', $type) {
  echo "You can't use string.";
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Worked for me
This only checks the type of the first element in the array and ignores the rest. Also you're also missing quotes around 'gettype'.
@jitendrapurohit What about if someone has an object or another array as a value? That's why I'm using return is_float($n) || is_int($n); in my answer.
OP has given an example for an array, and this works for validating it.
0

Try this

function sum_array($arr_sum){   
        $sum = 0;
        foreach($arr_sum as $value){
            if(!is_numeric($value)){
                echo 'Array Contain non numeric data';
                exit;
            }
            $sum += $value;
        }
        echo 'The sum is '.$sum;
    }

    $array_summation=["string",12.6,25.2,10,12];
    sum_array($array_summation);

1 Comment

Thanks Worked for me
0

PHP has a built-in array_sum function, which ignores non-numeric strings.

$arr = [1,2,'3','apple'];
var_dump(array_sum($arr)); // int(6)

Or if you really need them to produce an error you can use array_map:

function sum_array($arr) {
    if (array_sum(array_map('is_numeric', $arr)) !== count($arr)) {
        echo "You cannot use string";
        return;
    }
    echo array_sum($arr);
}
sum_array([1,2,'3','apple']); // You cannot use string
sum_array([1,2,'3','5.2']); // 11.2

Or if you want to make sure that all values in the array are either floats or integers, and not numeric strings, you can do:

function is_non_string_number($n) {
    return is_float($n) || is_int($n);
}
function sum_array($arr) {
    if (array_sum(array_map('is_non_string_number', $arr)) !== count($arr)) {
        echo "You cannot use string";
        return;
    }
    echo array_sum($arr);
}
sum_array([1,2,'3','apple']); // You cannot use string
sum_array([1,2,'3','5.2']); // You cannot use string
sum_array([1,2,3]); // 6

4 Comments

Thanks Worked for me
Note: This will allow numeric strings, such as "3.5". If you want to disallow that and only allow integers or floats, let me know and I'll update the answer.
Yes please specify that case also
@MughalGee Updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.