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

CSS Tutorial

PHP delete an element from an array



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;
?>



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

2245. PHP Tutorial - For loop
The PHP for loop is native function that lops throw a piece of code while a given conditional expression is true. It normally executes for a given number of times.
https://tutorialehtml.com/en/php-tutorial-for-loop/

1934. First script, php tutorial
First script php tutorial
http://www.agernic.com/php-tutorial/first-script.html

2602. PHP Install, php tutorial
PHP Install php tutorial
http://www.agernic.com/php-tutorial/php-install.html

1287. PHP INTRODUCTION
php tutorials and introduction. What is php and how to write php
https://www.plus2net.com/php_tutorial/php_introduction.php

3276. Login in PHP - simple login script with PHP MYSQL, php tutorial
Login in PHP - simple login script with PHP MYSQL php tutorial
https://www.agernic.com/php-tutorial/login-in-php-simple-login-script-with-php-mysql.html

3347. PHP Array - What is an Array?, php tutorial
PHP Array - What is an Array? php tutorial
https://www.agernic.com/php-tutorial/php-array-what-is-an-array.html

Your add here
How to get PHP errors to display?
How to prevent sql injection in php mysqli
How do I get PHP errors to display - PHP error_reporting()
mysql_query(): The mysql extension is deprecated and will be removed i
Function eregi_replace() is deprecated in php - How to fix
PHP delete an element from an array
Uncaught Error: Call to undefined function MYSQL_NUM_ROWS() in
Deprecated: mysql_connect() mysql extension is deprecated and will be
mysqli_fetch_array() expects parameter 1 to be mysqli_result boolean g
SQL IN Operator - example
SQL BETWEEN Operator
SQL like syntax and example
SQL WHERE AND & OR multiple conditions
SQL create table primary key autoincrement
PHP mysqli_connect () Function
SQL COUNT(), AVG() and SUM() Functions
Display data from SQL database with MySQLi into php - html table
SQL CREATE DATABASE - example
SQL mysql order by columns, by date, by asc, by desc, random with PHP
PHP mysqli_query() Function - mysqli query
SQL delete row - SQL delete table
SQL update - SQL update select
Insert into SQL - Insert into table SQL - Insert data in sql
Login in PHP - simple login script with PHP MYSQL