Sanooj T Sanooj T - 3 months ago 7x
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


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.