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

Array Remove

Is there an acutal function in PHP for removing an element of an array?

eg: array1 = ("zero", "one", "two", "three");
array1 = remove_element( array1, 2 );
// array1 now contains ("zero", "one", "three");

I have been trying the unset method but this seems to be corrupting my
array (which is holding a set of objects);

I can knock something together with aray_merge, array_splice and
similar but just didn't want to reinvent the wheel.

Thanks,

Rick
www.e-connected.com

Sep 30 '05 #1
4 6170
thehuby said the following on 30/09/2005 12:29:
Is there an acutal function in PHP for removing an element of an array?

eg: array1 = ("zero", "one", "two", "three");
array1 = remove_element( array1, 2 );
// array1 now contains ("zero", "one", "three");

I have been trying the unset method but this seems to be corrupting my
array (which is holding a set of objects);


Unset() is the method given in the manual.

In what way is it corrupting your array?
--
Oli
Sep 30 '05 #2
What's wrong with using array_splice($array, $index, 1)?

Sep 30 '05 #3
I was getting a message basically telling me that PHP could not use my
objects corectly. I got it working by using array_merge after unset.

No idea why it suddenly works after array_merge though but it will do.

My question was really just if anyone knew of a method/funciton to
remove them without having to mess around with splitting and merging
arrays.

Sep 30 '05 #4
There is no native PHP process to do the equivalent of "array_remove()"
as of yet. Your best would be to slap together a function using
unset($array['element']), as I've had to do.

Bear in mind that in future additions to PHP an "array_remove()"
function might someday exists, it's best that when you create it to
wrap the function around a "function_exists()" conditional block:

if (!function_exists('array_remove')) {
function array_remove($element, &$array, $willSearchKeys) {
// EXERCISE LEFT TO THE STUDENT!
}
}

Phil

Sep 30 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
7
by: Marc Bishop | last post by:
Hi can anyone help? I'm making a shopping cart and am stuck on removing an item from my array? The array is made : cArray(ITEM_NAME,cItem) = ProductName
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
1
by: swayze | last post by:
Hi there, We're using php 4.1 and it doesn't seem to have built in support for this. Coming from a dotnet background this surprised me...Anyways, thats a different topic altogether... I'm...
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...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
10
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: ...
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...
3
by: Barkingmadscot | last post by:
I am stuck, i can workout how to remove lines from an array I have loading a text file (a Log), I know which lines a need, but the logs can be upto 30K sometimes bigger. I found trying to...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.