470,870 Members | 1,416 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

simple delete from array?

Any way to do a simple delete from array?

In other words, what would be the *easiest* (and fastest php runtime)
way to delete "banana" from the following array:

$my_array = array(
"apple",
"banana",
"grape",
"lime"
);

.....so that $my_array will now be:

$my_array = array(
"apple",
"grape",
"lime"
);

Jul 17 '05 #1
2 25833
On Sat, 31 Jul 2004 04:44:31 GMT, Westcoast Sheri
<sh*********@nospamun8nospam.com> wrote:
Any way to do a simple delete from array?

In other words, what would be the *easiest* (and fastest php runtime)
way to delete "banana" from the following array:

$my_array = array(
"apple",
"banana",
"grape",
"lime"
);

....so that $my_array will now be:

$my_array = array(
"apple",
"grape",
"lime"
);


if you know the position (offset) of the element you want to delete
(in your example above, 'banana' is element 1 in the array):

array_splice($my_array, 1, 1);

if not:

array_splice($my_array, array_search('banana', $my_array), 1);

if you are using associative arrays you can use unset:

$my_array = array(
'apple' => 1
'banana' => 2,
'grape' => 3,
'lime' => 4
);
unset($my_array['banana']);

this appears not to work with non-associative arrays.

hth,
hc.
Jul 17 '05 #2
On Sat, 31 Jul 2004 04:44:31 GMT, Westcoast Sheri
<sh*********@nospamun8nospam.com> wrote:
Any way to do a simple delete from array?

In other words, what would be the *easiest* (and fastest php runtime)
way to delete "banana" from the following array:

$my_array = array(
"apple",
"banana",
"grape",
"lime"
);

....so that $my_array will now be:

$my_array = array(
"apple",
"grape",
"lime"
);


Use a second array and for each in the first array either insert it
into the second array or not depending on your criteria.

--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
DataBasix | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ۳
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Noah Spitzer-Williams | last post: by
13 posts views Thread by LRW | last post: by
8 posts views Thread by Ross A. Finlayson | last post: by
10 posts views Thread by Boni | last post: by
4 posts views Thread by Dmytro Bablinyuk | last post: by
1 post views Thread by Ironroot | last post: by
5 posts views Thread by michael | last post: by
29 posts views Thread by Jon Slaughter | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.