471,624 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

How to remove a item from a array?

Help
How can I remove an item from an array,
Example
Array[0]=3, array[1]=6 array[2]=7 array[3]=9

Is there a way to remove a item, say index 1, so the array would now
be

Array[0]=3 array[1]=7 array[2]=9 ?????

-Ted
Oct 6 '08 #1
6 5369
..oO(te*******@gmail.com)
>How can I remove an item from an array,
Example
Array[0]=3, array[1]=6 array[2]=7 array[3]=9

Is there a way to remove a item, say index 1, so the array would now
be

Array[0]=3 array[1]=7 array[2]=9 ?????
<?php
$array = array(3, 6, 7, 9);
var_dump($array);
unset($array[1]);
var_dump($array);
?>

Micha
Oct 6 '08 #2
te*******@gmail.com wrote:
Is there a way to remove a item, say index 1, so the array would now
be
unset($array[1]);

http://php.net/unset

--
----------------------------------
Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Proudly running Debian Linux with 2.6.26-1-amd64 kernel, KDE 3.5.9, and PHP
5.2.6-3 generating this signature.
Uptime: 18:58:06 up 46 days, 6:53, 2 users, load average: 0.41, 0.60,
0.63

Oct 6 '08 #3
te*******@gmail.com wrote:
Help
How can I remove an item from an array,
Example
Array[0]=3, array[1]=6 array[2]=7 array[3]=9

Is there a way to remove a item, say index 1, so the array would now
be

Array[0]=3 array[1]=7 array[2]=9 ?????

-Ted
<code>
$array = array(3,6,7,9);
unset($array[1]);
$array = array_values($array);
</code>

Joe
Oct 7 '08 #4

<te*******@gmail.comwrote in message
news:69**********************************@25g2000h sk.googlegroups.com...
Help
How can I remove an item from an array,
Example
Array[0]=3, array[1]=6 array[2]=7 array[3]=9

Is there a way to remove a item, say index 1, so the array would now
be

Array[0]=3 array[1]=7 array[2]=9 ?????
Ted,

I think everyone missed your point! Unsetting the array element looks to be
only half of your problem. The other half is that you want to reuse the
numeric index so that the array is sequencial without gaps. Here's a sample
you can use. It can be easily modified so that if the array's keys are
strings, you can preserve them too...however, that's beyond the scope here.
Notice, I assume that no matter what, you want a zero-based array. If your
array begins with element 5, then using the function will shift every
element down by 5. If you need something else, let me know...but, you'll
have to define exactly what you want to see. This sample simply takes what
you said you had, and processes the array to give the results you said you'd
expect.
Here's the code sample:

================

<?
function shrinkArray($array, $keys)
{
$newArray = array();
if (!is_array($array)){ $array = array($array); }
if (!is_array($keys)){ $keys = array($keys); }
foreach ($keys as $index){ unset($array[$index]); }
foreach ($array as $element){ $newArray[] = $element; }
return $newArray;
}
$array = array(
3 ,
6 ,
7 ,
9
);
echo '<pre style="font:10px;">' . print_r($array, true) . '</pre>';
$array = shrinkArray($array, 1);
echo '<pre style="font:10px;">' . print_r($array, true) . '</pre>';
?>

And here's the test output:

================
Array
(
[0] =3
[1] =6
[2] =7
[3] =9
)
Array
(
[0] =3
[1] =7
[2] =9
)
Oct 7 '08 #5
Oscar Arreyano escribi:
I think everyone missed your point! Unsetting the array element looks to be
only half of your problem. The other half is that you want to reuse the
numeric index so that the array is sequencial without gaps.
Joe Scylla's answer does exactly that, and in two lines of code:

unset($array[1]);
$array = array_values($array);
--
-- http://alvaro.es - lvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programacin web: http://bits.demogracia.com
-- Mi web de humor al bao Mara: http://www.demogracia.com
--
Oct 7 '08 #6

""lvaro G. Vicario"" <al****************@demogracia.comwrote in message
news:gc**********@huron.algomas.org...
Oscar Arreyano escribi:
>I think everyone missed your point! Unsetting the array element looks to
be only half of your problem. The other half is that you want to reuse
the numeric index so that the array is sequencial without gaps.

Joe Scylla's answer does exactly that, and in two lines of code:

unset($array[1]);
$array = array_values($array);
Sorry...I don't see Joe's post in my reader. :(

I like that better than my function though!

Cheers.
Oct 7 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Patrick G. | last post: by
12 posts views Thread by Sam Collett | last post: by
3 posts views Thread by Jeremy | last post: by
1 post views Thread by Jeff User | last post: by
4 posts views Thread by None | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.