Remove String Value From Array PHP Example

03-Apr-2023

.

Admin

Hi Guys,

Today, I will learn you how to remove string value from array in php. You can remove all string value or element on array in php.

Removing elements from a PHP array is actually simple and very easy. We have to delete string element from that array using the array_diff() and array_filter() function in PHP.

We can use unset() function which removes the element from an array. Array in all string value remove then you can 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

)

It will help you...

#Laravel 6

#PHP

#Laravel

#Laravel 7