Hi, I want to remove one element from array so i'm using unset function but it also remove that index number. I want to re-index after removing array. is there any solution?
6 16669 zmbd 5,501
Recognized Expert Moderator Expert
Post your code... (please remember to format it using the [Code/] formatting tool in the ribbon)
ADezii 8,834
Recognized Expert Expert
Try the array_splice() Function. In the following example, I remove 'black' from the Original Array and the Elements have appered to have been Re-indexed after the splice. -
<?php
-
$colors = Array("red", "green", "Blue", "purple", "orange", "black", "magenta", "cyan", "yellow", "violet", "brown");
-
echo 'Number of Elements in $colors: ' . count($colors);
-
-
echo "<BR><BR>";
-
-
$colors_removed = array_splice($colors, 5, 1);
-
echo 'Number of Elements in $colors_removed: ' . count($colors_removed) . "<BR>";
-
-
echo $colors[0] . "<BR>";
-
echo $colors[1] . "<BR>";
-
echo $colors[2] . "<BR>";
-
echo $colors[3] . "<BR>";
-
echo $colors[4] . "<BR>";
-
echo $colors[5] . "<BR>";
-
echo $colors[6] . "<BR>";
-
echo $colors[7] . "<BR>";
-
echo $colors[8] . "<BR>";
-
echo $colors[9] . "<BR>";
-
-
Number of Elements in $colors: 11
-
-
Number of Elements in $colors_removed: 1
-
red
-
green
-
Blue
-
purple
-
orange
-
magenta
-
cyan
-
yellow
-
violet
-
brown
-
pop - Removes from the End of an Array.
shift - Removes from the beginning of an Array.
splice - removes from a specific Array index.
filter - allows you to programatically remove elements from an Array.
dev7060 639
Recognized Expert Contributor
Using too many line breaks (<br>) is poor semantic HTML. Better use different logical blocks.
ADezii 8,834
Recognized Expert Expert @dev7060:
Thanks for the pointer. Brand new at PHP, not my area of expertise (usually live in the Access/VBA Forum).
pop - Removes from the End of an Array.
shift - Removes from the beginning of an Array.
splice - removes from a specific Array index.
filter - allows you to programmatically remove elements from an Array.
Regards,
Willjoe
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Pos yo, quién si no! |
last post by:
Hi Ng,
I created a div dynamically with the following code:
var d = document.createElement("DIV");
d.setAttribute("id","test");
document.body.appendChild(d);
I don't know how remove it...
|
by: Gerry Viator |
last post by:
Hi all,
The following is part of XML file, how do I search through the file and remove an
Element and all it's attributes. Example: TEST2? Of course I will pass the Element Name to remove
from a...
|
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...
|
by: Zytan |
last post by:
I want to make a zero element array. I know that Nothing is not the
same as a zero element array, since I can't get the length of, or
iterate through, an array = Nothing. I could make a zero...
|
by: Ron Peterson (012ED25E) |
last post by:
I ran into something like the following while looking into how the GNU
MP library implements its mpz_t type.
typedef struct {
int len;
char *buf;
} foo;
This is an interesting technique. ...
| |
by: mike-yue |
last post by:
The topic comes from a question:
Would you rather wait for the results of a quicksort, a linear search,
or a bubble sort on a 200000 element array?
1Quicksort
2Linear Search
3Bubble Sort
...
|
by: ganeshvkl |
last post by:
hi
my requirement is create control arrays at run time that i achived but i couldn't
remove control array object at run time visual Basic 6.0 i attached this code
Private Sub...
|
by: Jezternz |
last post by:
Ok, I want to remove an element from the standard flow, so its location is not affected or determined by other sibling elements, the only catch is, I dont want to position it using...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |