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 16646 zmbd 5,501
Expert Mod 4TB
Post your code... (please remember to format it using the [Code/] formatting tool in the ribbon)
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.
Using too many line breaks (<br>) is poor semantic HTML. Better use different logical blocks.
@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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |