473,385 Members | 1,546 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,385 software developers and data experts.

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 25992
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if...
13
by: LRW | last post by:
Having a problem getting a onSubmit function to work, to where it popsup a confirmation depending on which radiobutton is selected. Here's what I have: function checkdel() { if...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
10
by: Boni | last post by:
Dear all, I am wondering, that I can't find how to delete an element from an array. dim a as string() a=SomeString.split(",") for ... if _string="" then a.delete???? ...
4
by: Dmytro Bablinyuk | last post by:
I came across several possible ways of allocating memory for objects, for example: 1. malloc(sizeof(T)*3)/free - raw memory 2. new T/delete - buffer would be initialized to...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
1
by: Ironroot | last post by:
Hi all Ok in class to do we had to do the following and I keep on running into the same problem: class Array { public: Array(int size); //constructor - sets capacity to size void...
5
by: michael | last post by:
Hi All, I have the following: const int LENGTH = 5; void limitNameLength(string inText, char *&outText, int outLength){ if(static_cast<int>(inText.length()) outLength){ inText =...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.