Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete an array item

Status
Not open for further replies.

mickallen

Technical User
Jan 5, 2001
39
GB
Can anyone tell me the best way to delete an item within an array.
I want to search for a value within the array and then delete the item from the array if it meets certain criteria, which means the value could be anywhere within the array. I also want to be able to delete it totally from the array without leaving any indexes and blank values I have used the array_splice() function but wodered if there was another way as I'm having trouble getting it to work correctly.
 
If the keys of your array are integers, try using unset() on the array and then use array_splice() to renumber the keys.

For example:

$test[0] = '5';
$test[1] = '6';
$test[2] = '7';
$test[3] = '8';
$test[4] = '9';

unset($test[2]);
array_splice($test,0,0);
for($i=0; $i<count($test); $i++) {
print &quot;$i: $test[$i]<br>&quot;;
}

will return:

0: 5
1: 6
2: 8
3: 9


Calvin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top