Sanooj T Sanooj T - 1 year ago 99
PHP Question

Array only contain numbers in php

Hi guys i have an array


But i want to check if any other element than number exist in array
For example

These are my arrays


$a has numbers and variables

$b has numbers only

So my output is

check($a) should be false

check($b) should be true

Answer Source

is_numeric and simple foreach will be usful in this case.

Iterate over each element of array using foreach and check if the element is number or not using is_numeric() function.

Like this,

function check($array) {
     $allNumeric = true;
     foreach($array as $value) {
          if (!(is_numeric($key))) {
               $allNumeric = false;
     return $allNumeric;

Just break the loop as soon as you hit the first non-numeric value and return the Boolean flag used in loop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download