HOME | PHP Tutorial | CSS
facebook icon
AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP
PHP Tutorial

PHP forms
HTML Tutorial

CSS Tutorial

PHP delete an element from an array


Next Page >>
PHP eregi_replace()


If you want to delete just one array element you can use \unset() or alternative \array_splice(). How can you remove an element from an array when you know the elements name? for example:


Example: delete plums
<?php
$array = array('pears', 'apple', 'orange', 'strawberry', 'plums', 'kiwi');
?>

how to solve it?
Use array_search to get the key and remove it with unset if found:

Example: delete plums
<?php

$array = array('pears', 'apple', 'orange', 'strawberry', 'plums', 'kiwi');
if (($key = array_search('plums', $array)) !== false) {
unset($array[$key]);
}
echo $key;
?>



If there can be multiple items with the same value, you can use array_keys to get the keys to all items:

Example: delete plums
<?php
$array = array('pears', 'apple', 'orange', 'strawberry', 'plums', 'kiwi');
foreach (array_keys($array, 'plums') as $key) {
unset($array[$key]);
}
echo $key;
?>



Next Page >>
PHP eregi_replace()



php delete an element PHP delete an element from an array - php tutorial

3220. PHP Install, php tutorial
PHP Install php tutorial
https://www.agernic.com/php-tutorial/php-install.html

3216. PHP 5 Constants, php tutorial
PHP 5 Constants php tutorial
https://www.agernic.com/php-tutorial/php-constants.html

2603. PHP Tutorial - Learn PHP in simple and easy steps starting from basic to advanced, php tutorial
PHP Tutorial - Learn PHP in simple and easy steps starting from basic to advanced php tutorial
http://www.agernic.com/php-tutorial/php-tutorial-learn-php-in-simple-and-easy-steps-starting-from-basic-to-advanced.html

43. PHP Tutorial - An Ultimate Guide for Beginners
PHP Tutorials for Beginners - Learn how to create a dynamic and interactive website using the PHP 7 programming language in simple and easy steps.
https://www.tutorialrepublic.com/php-tutorial/

3352. PHP Database Driven Website
Use PHP to create a database driven website with this PHP tutorial.
https://www.quackit.com/php/tutorial/php_database_driven_website.cfm

3359. PHP Installation
Learn to install PHP with this PHP tutorial.
https://www.quackit.com/php/tutorial/php_installation.cfm

Your add here
PHP empty() Function

PHP strtolower() function

PHP ltrim() function

PHP implode() function

PHP str_split() function

PHP similar_text() function

PHP ucwords() function

PHP strstr() function

PHP strrchr() Function

PHP str_repeat() function

PHP rtrim() function

PHP printf() function

PHP strip_tags() function

PHP print() function

PHP substr() function

PHP html_entity_decode() function

Function eregi_replace() is deprecated in php - How to fix
CSS width and height - CSS Properties - Dimensions units
CSS Text Style - Text Font Size - Text Align
HTML div class tag attribute
How to create HTML link without underline
HTML Font Face