473,238 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 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 5459
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Patrick G. | last post by:
Greetings all: ASP VB, SQL Svr 2000 I am pulling data from 3 tables. table1 holds item details table2 holds publication types and the item id from table1 table3 holds category types and...
1
by: YT | last post by:
Howdy, Why the heck wouldn't this work: for each item in Session.Contents if NOT( Instr( item, "customer_" ) = 1 OR Instr( item, "user_" ) = 1 ) Then Session.Contents( item ) = ""...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
2
by: Mamatha | last post by:
Hi I have an application with listview.When i click on one button the data will be displayed like this in the listview: colA colB colC ----- ----- ------...
3
by: Jeremy | last post by:
I'm trying to parse a sentence and want to remove selected words from the middle. Consider: dim a() as object = split(mySentence) for each w in a() select case w case w.startswith("X")
1
by: Jeff User | last post by:
Hi ..net 1.1 I have a simple string array like string myArray = CallToWebServiceThat_Returns_Elements; I want to remove an item from the array and also remove its position. I can search the...
4
by: None | last post by:
Hi, I have declared array as Int ids = new int; In ArrayList we can remove specified index using RemoveAt(5) method. My question is how can we do this one with int array (single dimensional...
3
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.