note 26514 added to function.array-diff
| From: | jg at rack1 dot php dot net | Date: | Sat, 02 Nov 2002 07:54:11 +0000 |
| Subject: | note 26514 added to function.array-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-38898@lists.php.net to get a copy of this message | ||
I have found an interesting use for array_diff. I remember seing somebody having trouble deleting
from an array because it left "holes".
I thought about using array_diff to fix the problem. Try running this code and see what it does:
<?php
for($i=0;$i<4;$i++)
{
print("Deleting item $i from the array<BR>");
$my_array = array("value1","value2","value3","value4");
$my_array = array_diff($my_array,array_slice($my_array,$i,1));
foreach($my_array as $value)print(" -->$value<BR>\n");
}
?>
It seems to work for every member of the array. I'm not sure how efficient it would be on
really large arrays, though.
JG Estiot
--
http://www.php.net/manual/en/function.array-diff.php
http://master.php.net/manage/user-notes.php?action=edit+26514
http://master.php.net/manage/user-notes.php?action=delete+26514
http://master.php.net/manage/user-notes.php?action=reject+26514