DEV Community

loading...

Discussion on: Mastering PHP Arrays

Collapse
goodevilgenius profile image
Dan Jones
$names = ['Mike', 'Peter', 'Paul'];

//Remove array entry:
unset($names['Peter']);
Enter fullscreen mode Exit fullscreen mode

That unset will not do what you want it to do. You need one of the following:

// To preserve keys
unset($names[1]); 
// It is now => [0 => 'Mike', 2 => 'Paul']

// or to change keys
array_splice($name, 1, 1);
// It is now => [0 => 'Mike', 1 => 'Paul']
Enter fullscreen mode Exit fullscreen mode
Collapse
ericchapman profile image
Eric The Coder Author

Did not know that one... Thanks for the tips!