Remove String Value From Array PHP Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
Remove String Value From Array PHP Example

Today now in this blog, I will show you how to remove any string value from array in php. We can easily remove all string value or element on array in php application.

For removing elements from a PHP array is actually simple and also very easy. So we need to delete string element from that array by using the array_diff() and array_filter() function in PHP.

Now we can also use unset() function which removes the element from an array. So Array in all string value can remove then we just need to use bellow example.

Example 1 :

    $myArray = [1,"test",6.5,'demo',5,6];

    echo '<pre>';
    echo 'Remove Before Array :<br>';
    print_r($myArray);

    foreach ($myArray as $key => $val){
        $myArray = array_filter($myArray, function($val){

            return $val != is_string($val);

        });
    }

    echo 'Remove After Array :<br>';
    print_r($myArray);

Example 2 :

    $myArray = [1,"test",6.5,'demo',5,6];

    echo '<pre>';

    echo 'Remove Before Array :<br>';
    print_r($myArray);

    foreach ($myArray as $key => $val){

        if (gettype($val) == 'string'){

           unset($myArray[$key]);

        }
    }

    echo 'Remove After Array :<br>';
    print_r($myArray);

Output :

Remove Before Array :

Array
(
    [0] => 1
    [1] => test
    [2] => 6.5
    [3] => demo
    [4] => 5
    [5] => 6
)

Remove After Array :

Array
(
    [0] => 1
    [2] => 6.5
    [4] => 5
    [5] => 6
)

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →