473,499 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

remove element from array

1 New Member
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?
May 28 '22 #1
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)
May 29 '22 #2
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $colors = Array("red", "green", "Blue", "purple", "orange", "black", "magenta", "cyan", "yellow", "violet", "brown");
  3. echo 'Number of Elements in $colors: ' . count($colors);
  4.  
  5. echo "<BR><BR>";
  6.  
  7. $colors_removed = array_splice($colors, 5, 1);
  8. echo 'Number of Elements in $colors_removed: ' . count($colors_removed) . "<BR>";
  9.  
  10. echo $colors[0] . "<BR>";
  11. echo $colors[1] . "<BR>";
  12. echo $colors[2] . "<BR>";
  13. echo $colors[3] . "<BR>";
  14. echo $colors[4] . "<BR>";
  15. echo $colors[5] . "<BR>";
  16. echo $colors[6] . "<BR>";
  17. echo $colors[7] . "<BR>";
  18. echo $colors[8] . "<BR>";
  19. echo $colors[9] . "<BR>";
  20.  
Expand|Select|Wrap|Line Numbers
  1. Number of Elements in $colors: 11
  2.  
  3. Number of Elements in $colors_removed: 1
  4. red
  5. green
  6. Blue
  7. purple
  8. orange
  9. magenta
  10. cyan
  11. yellow
  12. violet
  13. brown
  14.  
Jul 9 '22 #3
paanjii2
1 New Member
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.
Jul 10 '22 #4
dev7060
639 Recognized Expert Contributor
Using too many line breaks (<br>) is poor semantic HTML. Better use different logical blocks.
Jul 13 '22 #5
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).
Jul 13 '22 #6
Willjoe
12 New Member
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
Jul 14 '22 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
20226
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...
8
10477
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...
3
3792
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...
33
8139
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...
17
3598
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. ...
26
2549
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 ...
0
1291
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...
3
6378
Jezternz
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...
0
7130
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
7007
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...
0
7171
Oralloy
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,...
0
7220
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...
1
6893
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...
0
3098
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...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
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...

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.