remove 1 value from array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fxchain
Forum Newbie
Posts: 9
Joined: Tue Dec 09, 2003 12:05 pm

remove 1 value from array

Post by fxchain »

Imagine this array:

Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
)


Is there an easy way to remove the banana value so it looks like this:

Array
(
[0] => orange
[1] => apple
[2] => raspberry
)

Thank you for any help.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

niceguyeddie
Forum Newbie
Posts: 13
Joined: Fri Nov 28, 2003 7:12 am
Location: Westminster, MD

Post by niceguyeddie »

http://www.php.net/manual/en/function.array-pop.php

Will also work, if you reorder the array.
User avatar
Scribbler
Forum Newbie
Posts: 5
Joined: Sat Nov 08, 2003 11:50 am
Location: Aurora CO
Contact:

Post by Scribbler »

I believe that's a job for array_splice() not array_slice().

array_splice(array data, integer start, integer stop, array insert_data)


If you leave insert_data out, then it removes the array items without replacing them with the new data. If integer stop is a negative number, then it'll reference from the end of the array backward. If stop is positive, but less than start, then it'll insert items at integer_start without removing any.

So to remove the banana you'd use

array_splice($myArray, 1, 1);
Post Reply