By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,967 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,967 IT Pros & Developers. It's quick & easy.

How to remove a item from a array?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
..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

P: n/a
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

P: n/a
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

P: n/a

<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

P: n/a
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 programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Oct 7 '08 #6

P: n/a

""Á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.