# Deleting from the middle of an array

**URL:** <https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309>\
**Category:** Uncategorized\
**Created:** [August 13, 2004, 10:31am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309 "2004-08-13T10:31:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ViNc3](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@ViNc3](https://forum.kirupa.com/u/ViNc3)\
**Post date:** [August 13, 2004, 10:31am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/1 "2004-08-13T10:31:04Z")

</div>

is there a way to delete from the middle of an array?

like if i wanted to take bananas off grocery

```php
  
grocery = ["apples", "peach", "bananas", "mangos", "pears", "tomatoes]

```

what’s the code to use?

---

<div class="post-metadata">

**Author:** ![APDesign](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@APDesign](https://forum.kirupa.com/u/APDesign)\
**Post date:** [August 13, 2004, 10:32am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/2 "2004-08-13T10:32:47Z")

</div>

```auto

grocery.splice(2,1);

```

It permanently removes it though, so watch out.

edit: incase you don’t know, it’s 2 and not 3 because the array index starts at 0

ie: 0-\>apples, 1-\>peach, 2-\>banannas, …

---

<div class="post-metadata">

**Author:** ![ViNc3](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@ViNc3](https://forum.kirupa.com/u/ViNc3)\
**Post date:** [August 13, 2004, 10:37am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/3 "2004-08-13T10:37:53Z")

</div>

ic, and to add? (since we’re on the topic)

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [August 13, 2004, 10:40am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/4 "2004-08-13T10:40:41Z")

</div>

also use splice. Look it up in Flash help. learn it. love it. use it.

---

<div class="post-metadata">

**Author:** ![arcaRox](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@arcaRox](https://forum.kirupa.com/u/arcaRox)\
**Post date:** [August 13, 2004, 10:43am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/5 "2004-08-13T10:43:34Z")

</div>

That should be,

```auto

grocery.splice (2, 1);

```

Just using splice(2) will remove all the items from the array starting from bananas index 2. 1 indicates that you only want to remove only one item.

---

<div class="post-metadata">

**Author:** ![ViNc3](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@ViNc3](https://forum.kirupa.com/u/ViNc3)\
**Post date:** [August 13, 2004, 10:43am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/6 "2004-08-13T10:43:45Z")

</div>

lol, thx sen 😛

I’ve encountered a prob:

```php
  
grocery = ["apples", "peach", "bananas", "mangos", "pears", "tomatoes"];
trace(grocery);
//returns "apples, peach, bananas, mangos, pears, tomatoes"
grocery.splice(2)
trace (grocery)
//returns "apples,peach"

```

I only wanted to take out bananas ONLY…

---

<div class="post-metadata">

**Author:** ![APDesign](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@APDesign](https://forum.kirupa.com/u/APDesign)\
**Post date:** [August 13, 2004, 10:44am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/7 "2004-08-13T10:44:54Z")

</div>

> [@arcaRox](#):
>
> That should be,
> 
> ```auto
> 
> grocery.splice (2, 1);
> 
> ```
> 
> Just using splice(2) will remove all the items from the array starting from bananas index 2. 1 indicates that you only want to remove only one item.

Yeah, you’re totally right, sorry about that… I’ll edit my post for future peoples sake. This will fix your problem too vince.

---

<div class="post-metadata">

**Author:** ![ViNc3](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@ViNc3](https://forum.kirupa.com/u/ViNc3)\
**Post date:** [August 13, 2004, 10:44am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/8 "2004-08-13T10:44:57Z")

</div>

> [@arcaRox](#):
>
> That should be,
> 
> ```auto
> 
> grocery.splice (2, 1);
> 
> ```
> 
> Just using splice(2) will remove all the items from the array starting from bananas index 2. 1 indicates that you only want to remove only one item.

yea…is there a way to only take out bananas… **_only_**!? 😛

---

<div class="post-metadata">

**Author:** ![APDesign](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@APDesign](https://forum.kirupa.com/u/APDesign)\
**Post date:** [August 13, 2004, 10:46am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/9 "2004-08-13T10:46:13Z")

</div>

yeah, splice(2,1);

the first number 2 tells which index to delete, and the second number tells how many to delete. Sorry I messed you up in my first reply ☹ Hopefully all is better now.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [August 13, 2004, 10:46am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/10 "2004-08-13T10:46:43Z")

</div>

> [@ViNc3](#):
>
> yea…is there a way to only take out bananas… **_only_**!? 😛

a loop

---

<div class="post-metadata">

**Author:** ![ViNc3](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@ViNc3](https://forum.kirupa.com/u/ViNc3)\
**Post date:** [August 13, 2004, 10:48am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/11 "2004-08-13T10:48:40Z")

</div>

no probs! Thx all…haha…

a loop, sen?:h:

---

<div class="post-metadata">

**Author:** ![APDesign](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@APDesign](https://forum.kirupa.com/u/APDesign)\
**Post date:** [August 13, 2004, 10:51am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/12 "2004-08-13T10:51:25Z")

</div>

I think it must be a joke, but I’m just too dense (/tired, it’s 5:51 a.m. and no sleep for me ☹ ) to get it.

---

<div class="post-metadata">

**Author:** ![ViNc3](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@ViNc3](https://forum.kirupa.com/u/ViNc3)\
**Post date:** [August 13, 2004, 10:53am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/13 "2004-08-13T10:53:49Z")

</div>

What are u doing up so late, man! Get to sleep!! :z:

---

<div class="post-metadata">

**Author:** ![APDesign](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@APDesign](https://forum.kirupa.com/u/APDesign)\
**Post date:** [August 13, 2004, 10:55am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/14 "2004-08-13T10:55:15Z")

</div>

Helping you!!! Well… failing at helping you ☹

---

<div class="post-metadata">

**Author:** ![ViNc3](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@ViNc3](https://forum.kirupa.com/u/ViNc3)\
**Post date:** [August 13, 2004, 10:57am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/15 "2004-08-13T10:57:46Z")

</div>

lol…people forget! 😉

I got this question from this thread actually:

[http://www.kirupaforum.com/forums/showthread.php?t=66381](http://www.kirupaforum.com/forums/showthread.php?t=66381)

And look who was there 😛

---

<div class="post-metadata">

**Author:** ![ViNc3](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@ViNc3](https://forum.kirupa.com/u/ViNc3)\
**Post date:** [August 13, 2004, 11:00am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/16 "2004-08-13T11:00:11Z")

</div>

there should be a tutorial about this…or added to the already-existing array tutorial…but i don’t know how to use PHP :(…hehe 😛

---

<div class="post-metadata">

**Author:** ![APDesign](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@APDesign](https://forum.kirupa.com/u/APDesign)\
**Post date:** [August 13, 2004, 11:00am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/17 "2004-08-13T11:00:20Z")

</div>

haha, yep… I’m just kind of hanging out waiting for new posts 😉

I think I’m going to take a break though, maybe do some ASing.

If you hit F1 (I think, or Help\>ActionScript Dictoinary, or something) you can get the Flash help, searching the AS dictionary for Array and such is really the best way to learn.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [August 13, 2004, 11:01am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/18 "2004-08-13T11:01:02Z")

</div>

LOOP!!! If you’re working with arrays, you have to know (well should know) how to loop.

```php
// ActionScript:
function RemoveLikeElements(_array, _element){
	var i = _array.length;
	while (i--) if (_array* == _element) _array.splice(i,1);
}

grocery = ["bananas", "apples", "peach", "bananas", "mangos", "pears", "bananas", "tomatoes"];
trace(grocery);
// bananas,apples,peach,bananas,mangos,pears,bananas,tomatoes
RemoveLikeElements(grocery, "bananas");
trace(grocery);
// apples,peach,mangos,pears,tomatoes

```

---

<div class="post-metadata">

**Author:** ![ViNc3](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@ViNc3](https://forum.kirupa.com/u/ViNc3)\
**Post date:** [August 13, 2004, 11:06am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/19 "2004-08-13T11:06:25Z")

</div>

lol! sen, you’re post helped me understand about functions more than anything :thumb: 😛

---

<div class="post-metadata">

**Author:** ![APDesign](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@APDesign](https://forum.kirupa.com/u/APDesign)\
**Post date:** [August 13, 2004, 11:08am UTC](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309/20 "2004-08-13T11:08:56Z")

</div>

ah, i see… remove like elements in one fell swoop.

[Next page](https://forum.kirupa.com/t/deleting-from-the-middle-of-an-array/119309.md?page=2)
