Removing strings or non-integer values from an array in PHP – Simple and Easy Method

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.

Related Posts

forest fire

Forest Fire – Techgig, All testcases passed. Challenge #1

Forest Fire (100 Marks) Challenge 1: Forest fire You are camping in a forest area at night. You are living with the forest officers to experience their challenges…

the magic wand

The Magic Wand – TechGig PHP answer, all test cases passed. Challenge #2

The Magic Wand – TechGig PHP answer – all testcases passed The Magic Wand (100 Marks) You are a wizard who possesses the magic wand that can be…

PHP interview Questions and Answers

What is PHP? Answer: PHP stands for Hypertext Preprocessor. It is a widely used server-side scripting language primarily designed for web development. PHP code is embedded within…

install php

A Step-by-Step Guide on How to Install PHP

Install PHP : A Step-by-Step Guide on How to Install PHP Introduction: PHP is a widely used server-side scripting language that powers countless websites and web applications….

what is php

What is PHP? Understanding PHP: The Powerhouse of Web Development

Introduction: What is PHP? Understanding PHP: The Powerhouse of Web Development: In the world of web development, PHP stands tall as one of the most widely-used and…

php form

Complete Php form example

Here we are going to check how to create a php form and insert its values to database table. In this example we will be submitting the…

This Post Has 6 Comments

  1. Thanks for a marvelous posting! I quite enjoyed reading it, you happen to be a great author.
    I will make sure to bookmark your blog and may come back at some point.
    I want to encourage continue your great job, have a nice afternoon!

Leave a Reply

Your email address will not be published. Required fields are marked *