How to Remove Value If Exists From Array in PHP?

03-Apr-2023

.

Admin

How to Remove Value If Exists From Array in PHP?

Hi Dev,

This article will provide example of how to array remove value if exists in php. In this article, we will remove key and value if exists using php. I explained php array remove value if exists with code examples. you will learn php unset array if exists example. Let's see bellow example php deleting elements of an array by unset.

If the array contains only a single element with the specified value, you can use the array_search() function with unset() function to remove it from an array.

Example :


index.php

<?php

$myArray = ['Nicesnippets', 'Mywebtuts', 'Linus', 'Itsolutionstuff'];

$remove = array_search('Linus', $myArray);

print_r('Linus found at: '.$remove);

// Remove from array

unset($myArray[$remove]);

print_r($myArray);

?>

Output:

Linus found at: 2Array

(

[0] => Nicesnippets

[1] => Mywebtuts

[3] => Itsolutionstuff

)

I hope it could help you...

#PHP