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.
Related Posts:
- Difference between “as $key => $value” and “as $value” in PHP foreach
- Array to String PHP?
- “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
- Convert a PHP object to an associative array
- How to echo or print an array in PHP?
- Deleting an element from an array in PHP
- How to insert an item at the beginning of an array in PHP?
- what is a good method to sanitize the whole $_POST array in php?
- Sort array of objects by object fields
- In_array not working
- PHP multidimensional array search by value
- Fatal error: Cannot use object of type mysqli_result
- php foreach with multidimensional array
- Convert php array to Javascript
- PHP reindex array?
- php – How do I fix this illegal offset type error
- Mysql where id is in array
- How to convert array to SimpleXML
- Array of PHP Objects
- Is there a function to make a copy of a PHP array to another?
- How to Sort a Multi-dimensional Array by Value
- PHP array printing using a loop
- Fastest way to implode an associative array with keys
- PHP Multiple Checkbox Array
- Display array values in PHP
- Reset PHP Array Index
- How to push both value and key into PHP array
- Remove empty array elements
- How to avoid Request Entity Too Large 413 error
- Should I use mysqli_real_escape string() or mysql_real_escape_string() for form data?
- PDOException SQLSTATE[HY000] [2002] No such file or directory
- Form submission: PHP S_SESSION statements within a foreach loop
- PDOException: SQLSTATE[HY000] [2002] No such file or directory
- How do I get a file extension in PHP?
- mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
- XAMPP Port 80 in use by “Unable to open process” with PID 4
- How to parse a CSV file using PHP
- Cannot use object of type stdClass as array?
- Is a new line = \n OR \r\n?
- Email Not Sending from Byethost Hosting
- Illegal string offset Warning PHP
- Can curl make a connection to any TCP ports, not just HTTP/HTTPS?
- mysqli_real_connect(): (HY000/2002): No such file or directory
- Converting string to Date and DateTime
- How can I do ‘insert if not exists’ in MySQL?
- How to solve PHP error ‘Notice: Array to string conversion in…’
- The localhost page isn’t working localhost is currently unable to handle this request. HTTP ERROR 500
- What is the difference between the ‘www’ folder and ‘htdocs’ folder?
- Notice: Trying to get property of non-object error
- undefined offset PHP error
- Call to undefined function mysql_query() with Login
- What is Options +FollowSymLinks?
- Fatal error: Call to a member function prepare() on null
- PHP mail not working for some reason
- move_uploaded_file gives “failed to open stream: Permission denied” error
- Call to undefined method mysqli_stmt::get_result
- How to reformat date in PHP?
- PHP convert XML to JSON
- Cannot simply use PostgreSQL table name (“relation does not exist”)
- How do I expire a PHP session after 30 minutes?
- How to make a countdown using PHP
- Package php5 have no installation candidate (Ubuntu 16.04)
- How can I do ‘insert if not exists’ in MySQL?
- WordPress child theme style.css not working
- Prioritize Visible Content WordPress
- mysqli_select_db() expects parameter 1 to be mysqli, string given
- Unexpected Exception: SQLSTATE[HY000] [1045] Access denied for user ****@’localhost’ (using password: YES)
- Remove all special characters from a string
- how to convert object into string in php
- No Application Encryption Key Has Been Specified
- proper way to logout from a session in PHP
- PHP json_decode() returns NULL with valid JSON?
- Why do I have to run “composer dump-autoload” command to make migrations work in laravel?
- how to get date of yesterday using php?
- file_get_contents(“php://input”) or $HTTP_RAW_POST_DATA, which one is better to get the body of JSON request?
- file_get_contents( ) not working
- How can I fetch all items from a DynamoDB table without specifying the primary key?
- How to extract and access data from JSON with PHP?
- Authenticating in PHP using LDAP through Active Directory
- What is /var/www/html? [closed]
- What is wrong in chrome “net : Failed to load resource: net::ERR_SPDY_PROTOCOL_ERROR”?
- Formatting Phone Numbers in PHP
- “End of script output before headers” in Apache + PHP
- What is unexpected T_VARIABLE in PHP?
- How to close Browser Tab After Submitting a Form?
- How to make DROP INDEX IF EXISTS for mysql?
- get type in php returns “object” and not the object type
- How to find out if you’re using HTTPS without $_SERVER[‘HTTPS’]
- What is the meaning of sprintf(): Too few arguments
- How to use $_SERVER[‘HTTP_REFERER’] correctly in php?
- what is $_SERVER[‘QUERY_STRING’] ? how it works?
- PHP Error : Fatal error: Constant expression contains invalid operations
- Paypal IPN Verification Postback with HTTPS
- Using PHP 7 with WAMP
- Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given… what I do wrong?
- PHP: bootstrap.php
- Php create a file if not exists
- Page redirect after certain time PHP
- Check if $_POST exists
- PHP – Fatal error: Unsupported operand types