A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearDelete an element from an array - page 1
User InfoPosts
Delete an element from an array#1
Is there an easy way to delete an element from a PHP array, such that foreach ($array) no longer includes that element?

I thought that setting it to null would do it, but apparently not.

posted date: 2008-12-15 12:28:00


Re: Delete an element from an array#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-15 12:28:01


Re: Delete an element from an array#3
You use unset:<?php$x = array(5, 6);unset($x[0]);var_dump($x);?>… this yields:array(1) { [1]=> int(6)}(Notice that the index remains unchanged.)

posted date: 2008-12-15 12:30:00


Re: Delete an element from an array#4
unset($array[$index]);

posted date: 2008-12-15 12:31:00


Re: Delete an element from an array#5
It should be noted that unset() will keep indexes untouched, which is what you'd expect when using string indexes (array as hashtable), but can be quite surprising when dealing with integer indexed arrays:$array = array(0, 1, 2, 3);unset($array[2]);var_dump($array);/* array(3) { [0]=> int(0) [1]=> int(1) [3]=> int(3)} */$array = array(0, 1, 2, 3);array_splice($array, 2, 1);var_dump($array);/* array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(3)} */So array_splice() can be used if you'd like to normalize your integer keys. Another option is using array_values() after unset():$array = array(0, 1, 2, 3);unset($array[2]);$array = array_values($array);var_dump($array);/* array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(3)} */

posted date: 2008-12-15 13:25:00


Re: Delete an element from an array#6
Also, for a named element:unset($array["elementName"]);

posted date: 2010-07-30 17:06:00


Re: Delete an element from an array#7
Don't you think you should have choosen Stefan Gehrig answer www.momige.com/369602/… ?!

posted date: 2011-05-27 06:17:00


Re: Delete an element from an array#8
$a = array("A"=>1, "B"=>2, "C"=>"a");print_r($a);unset($a["B"]);print_r($a); gives (formatted): Array ( [A] => 1 [B] => 2 [C] => a ), Array ( [A] => 1 [C] => a )

posted date: 2011-06-08 17:50:00


Re: Delete an element from an array#9
// our initial array $arr = array("blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red"); print_r($arr); // remove the elements who's values are yellow or red $arr = array_diff($arr, array("yellow", "red")); print_r($arr); This is the output from the code above: Array( [0] => blue [1] => green [2] => red [3] => yellow [4] => green [5] => orange [6] => yellow [7] => indigo [8] => red)Array( [0] => blue [1] => green [4] => green [5] => orange [7] => indigo)Now, array_values() will reindex a numerical array nicely, but will remove all key strings from the array and replace them with numbers. If you need to preserve the key names (strings), or reindex the array if all keys are numerical, use array_merge():$arr = array_merge(array_diff($arr, array("yellow", "red")));print_r($arr);outputs Array( [0] => blue [1] => green [2] => green [3] => orange [4] => indigo)

posted date: 2011-08-02 07:57:00


Re: Delete an element from an array#10
$key = array_search($needle,$array);if($key!==false){ unset($array[$key]);}

posted date: 2011-11-15 03:29:00


Re: Delete an element from an array#11
It seems you cannot unset array elements indexed by a string (generates "Fatal error: Cannot unset string offsets"). I dont think this was always the case, but certainly as of PHP 5.3.10 and probably earlier

posted date: 2012-04-05 16:29:00


Re: Delete an element from an array#12
It's worth noting that when you're using array_splice() you need to know the OFFSET, not the key, but the offset (!) of whatever element you wish to remove

posted date: 2012-06-08 13:57:00


select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.