473,372 Members | 1,555 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,372 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 16611
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.