Using array_search() and unset, try the following:

if (($key = array_search($del_val, $messages)) !== false) {
    unset($messages[$key]);
}

array_search() returns the key of the element it finds, which can be used to remove that element from the original array using unset(). It will return FALSE on failure, however it can return a false-y value on success (your key may be 0 for example), which is why the strict comparison !== operator is used.

The if() statement will check whether array_search() returned a value, and will only perform an action if it did.

Answer from Bojangles on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_arrays_remove.asp
PHP Remove Array Items
In PHP, you can remove/delete array items with several different functions: array_splice() - removes a portion of the array starting from a start position and length ยท unset() - removes the element associated with a specific key ยท array_diff() ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ php โ€บ deleting elements from an array in php
Deleting elements from an array in PHP | Sentry
The unset() function, which removes the element by its key and removes the elementโ€™s index. The array_splice() function is the most versatile and direct solution for deleting an element from both indexed arrays and associative arrays (with ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-delete-an-element-from-an-array-in-php
How to delete an Element From an Array in PHP ? - GeeksforGeeks
July 23, 2025 - Example 3: This example uses array_diff() function to delete the elements. Please note that the array values are passed as second parameter not the index. This function takes array parameter by value not reference and returns an array as output.
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.array-splice.php
PHP: array_splice - Manual
Note that the function does not use a reference to the original array but returns a new array (I see absolutely no reason how the performance would be increased by using a reference when modifying an array through PHP script code). ... To remove elements from an array, based on array values: <?php $i_to_remove=array(); foreach($array_to_prune as $i=>$value){ if(cond_to_delete($value)) $i_to_remove[]=$i; } foreach($i_to_remove as $j=>$i) array_splice($array_to_prune,$i-$j,1); ?>
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ different-ways-to-delete-an-item-from-an-array-in-php
Different Ways to Delete an Item From an Array in PHP - GeeksforGeeks
July 23, 2025 - Example: The loop iterates through the array, and unset() removes elements that match the specified value. ... <?php $arr = array(1, 2, 3, 4, 3, 5); $remVal = 3; foreach ($arr as $key => $value) { if ($value == $remVal) { // Remove the element ...
๐ŸŒ
ItSolutionstuff
itsolutionstuff.com โ€บ post โ€บ php-remove-element-from-array-by-value-exampleexample.html
PHP Remove Element from Array by Value Example - ItSolutionstuff.com
May 14, 2024 - I will explain step by step tutorial php remove element from array by value. i would like to share with you how to remove array element by value in php. you will learn php remove item from array by value. you will learn php remove item from array based on value.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-delete-php-array-element-by-value-not-key.php
How to Delete PHP Array Element by Value Not Key
<?php // Sample indexed array $array1 = array(1, 2, 3, 4, 5); // Search value and delete if(($key = array_search(4, $array1)) !== false) { unset($array1[$key]); } print_r($array1); echo "<br>"; // Sample eassociative array $array2 = array("a" => "Apple", "b" => "Ball", "c" => "Cat"); // Search value and delete if(($key = array_search("Cat", $array2)) !== false) { unset($array2[$key]); } print_r($array2); ?>
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ Which-function-is-used-to-remove-or-delete-elements-from-an-array-in-PHP
Which function is used to remove or delete elements from an array in PHP? - Quora
Answer (1 of 3): Here are two ways to remove elements from arrays in PHP 1- unset() removes the element by its index [code]$arr = ["one", "two", "two", "three", "three", "four"]; unset($arr[2]); // remove element of index 2 [/code]output: [code]$arr = ["one", "two", "three", "three", "four"];...
๐ŸŒ
Medium
medium.com โ€บ @aliusama.dev โ€บ deleting-an-element-from-an-array-in-php-9676a3438d0f
Deleting an Element from an Array in PHP | by AliUsama Dev | Medium
January 26, 2024 - If you need to remove an element by its index, unset() and array_splice() are good options. If removal is based on values, consider using array_diff() or a combination of array_merge() and array_diff().
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ remove-specific-element-from-an-array-in-php
Remove Specific Element from an Array in PHP - GeeksforGeeks
July 23, 2025 - Below are the approaches to remove ... array in PHP: ... The unset() function is used to remove an element from an array by its key. This method works well when you know the key of the element to be removed.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ removing-array-element-and-re-indexing-in-php
Removing Array Element and Re-Indexing in PHP - GeeksforGeeks
July 11, 2025 - <?php $arr1 = array( 'geeks', // ... var_dump($arr1); ?> ... Using `array_filter()` with a callback function allows you to remove specific elements from an array by filtering out elements based on a condition....
๐ŸŒ
Reactgo
reactgo.com โ€บ home โ€บ how to remove the element from an array by value in php
How to remove the element from an array by value in PHP | Reactgo
June 11, 2023 - Here is an example, that removes the second element "green" from the colors array by value. <?php $colors = array("red", "green", "yellow", "orange"); if (($key = array_search("green", $colors)) !== false) { unset($colors[$key]); } print_r($colors) ?>
๐ŸŒ
Plus2Net
plus2net.com โ€บ php_tutorial โ€บ array_unset.php
Deleting key & value element of array by using unset command in PHP
February 5, 2000 - Note that array_diff function takes two arrays as input. Here is the code , we want to remove d from our $input array. $new_array=array_diff($input,array("d")); We can also use like this to remove more elements by using its value.
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
Delete element from within an array - PHP - SitePoint Forums | Web Development & Design Community
September 25, 2011 - I have tried looking and cannot find it. If I have an array like this tt=array(10,2,23,14) How do I eliminate the number 23 from the array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-perform-array-delete-by-value-not-key-in-php
How to perform Array Delete by Value Not Key in PHP ? - GeeksforGeeks
July 23, 2025 - To delete array elements by value using array_values() with array_splice() in PHP, find the key of the value, splice the array at that key, then reindex the array using array_values(). Example: In this example we declares an associative array, ...
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-delete-an-element-from-an-array-in-php.php
How to Delete an Element from an Array in PHP
If you see the above example carefully ... deleting the value from the numeric array (line no-8). To fix this you can use the array_splice() function. It takes three parameters: an array, offset (where to start), and length (number of elements ...
๐ŸŒ
Uptimia
uptimia.com โ€บ home โ€บ questions โ€บ how to delete an element from an array in php?
How to Delete an Element from an Array in PHP?
May 20, 2024 - Learn how to remove an element from an array in PHP using the unset() function or array_splice(). Discover efficient ways to delete array elements by value or index.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ php-remove-object-from-array
PHP: Remove object from array
$index = 2; $objectarray = array( ... 'defabc', 'value' => '03n2') ); var_dump($objectarray); foreach ($objectarray as $key => $object) { if ($key == $index) { unset($objectarray[$index]); } } var_dump($objectarray);...