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

remove element from array

1
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 16646
zmbd
5,501 Expert Mod 4TB
Post your code... (please remember to format it using the [Code/] formatting tool in the ribbon)
May 29 '22 #2
ADezii
8,834 Expert 8TB
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 Bit
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
636 Expert 512MB
Using too many line breaks (<br>) is poor semantic HTML. Better use different logical blocks.
Jul 13 '22 #5
ADezii
8,834 Expert 8TB
@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 Byte
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
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
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
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
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
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
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
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
0
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
tracyyun
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...
0
agi2029
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,...

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.