AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...