SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 

PHP deleting elements of an array by unset ( key or value )


We can remove an element from an array by using unset command. However unset command is used to destroy any other variable and same way we can use delete any element of an array. This unset command takes the array key as input and removed that element from the array. After removal the associated key and value does not change. Let us see one simple example of this.

unset($input[3]);

Here we are removing the element with key=3. If the array has 7 elements and if try to delete 9th element then unset command will not return any error but nothing will be deleted.

Here is an example how unset command is used.

$input=array(a,b,c,d,e,f,g);
unset($input[3]);
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}

As we are deleting the third element the output of above code is here.

0 -> a
1 -> b
2 -> c
4 -> e
5 -> f
6 -> g

How to delete an element by using value ( not key ) from an array

In the above examples we have used key if the array as input to remove the element from the array. If we don't know the key and we know the value then how to remove the element? There is no direct function to do this but we can use array_diff function to remove the element by using the value. Note that array_diff function takes two arrays as input. Here is the code , we want to remove d from our $input array.

$new_array=array_diff($input,array("d"));

We can also use like this to remove more elements by using its value.

$remove=array(c,f);
$new_array=array_diff($input,$remove);

If you want you can display & check our elements like this.

while (list ($key, $val) = each ($new_array)) {
echo "$key -> $val <br>";
}

The output of the above command is here

0 -> a
1 -> b
3 -> d
4 -> e
6 -> g


Further readings
 PHP array
Array functions in PHP
array : Creating an Array
array_diff Difference of two arrays
array_count_values counting the frequency of values inside an array
count : sizeof Array Size or length
array_push : Adding element to an Array
array_sum : Array Sum of all elements
Displaying the elements of an array
Array checkbox
in_array : IN Array to search for elements inside an array
array_rand : Random elements of Array
array_unique : Array Unique values
Breaking of strings to create array using split command
Session Array to maintain data in different pages
unset : Deleting elements of an array by using its key or value
sort: Simple sorting of PHP array
rsort: Reverse sorting of PHP array
asort: Maintaining index of the elements after sorting
arsort: Sorting in reverse order for a PHP array
ksort: Array key Sorting in PHP
krsort: Array reverse key Sorting in PHP
usort: Sorting array by using user defined function

























Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
Array Functions
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.