1

Please I need to delete or update a data stored in an array for example:

$array = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
$data_to_delete = 'Tuesday';
//possible explanation of code to run
if(in_array($data_to_delete, $array)){
    //run code to delete $data_to_delete from the $array
}
2
  • 1
    array_filter potentially? Commented May 9, 2016 at 21:06
  • Yeah. Thanks Jon... That's what I need Commented May 9, 2016 at 21:09

2 Answers 2

3

Use the unset() command.

So to delete "Tuesday" you use:

$key = array_search("Tuesday", $array); 
if( $key !== false ) {
    unset(array_search("Tuesday", $array));
}

If you didn't know, array_search() returns key of the string it has found, so it is the equivalent of:

unset($array[1]);
Sign up to request clarification or add additional context in comments.

Comments

2
if(($i = array_search($data_to_delete, $array, true)) !== false) {
    unset($array[$i]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.