Removing non-integer entries from array in php

Here we are going to discuss how to remove strings or non-integer values from an array in php.

For this we need to use one simple array function in php called array_filter() . it always filters an array using a callback function.

Syntax :

array_filter(array, callbackfunction, flag)

Parameters :

  • array :- required, is the array to be filtered
  • callbackfunction :- optional, the callback function
  • flag :- optional, Specifies what arguments are sent to callback

Example

array([0]  => 1,
      [1]  => 'one',
      [2]  => 2,
      [3]  => 'two',
      [4]  => 3);

Expected Result :

array([0]  => 1,
      [1]  => 2,
      [2]  => 3);

Solution :

$result = array_filter($array, 'is_numeric');

Here you dont have to do any other steps as ‘is_numeric’ is a built in function, all you have to do is just call this line.

Click me for more intersting PHP Solutions.

Leave A Comment