Home php Remove String Value From Array PHP Example

Remove String Value From Array PHP Example

by Shahriar Sagor
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

close

You may also like