Delete an element from an array
Is there an easy way to delete an element from a PHP array, such that foreach ($array)
no longer includes that element?
I thought that setting it to null would do it, but apparently not.
Array
- asked 9 years ago
- B Butts
1Answer
Data structure’s C program to delete an element from the array. Enter the array size and then it’s elements. After this enter the location of the element you want to delete and the item will be deleted. Below is the code of the above program
/* Program in C to delete an element from the array */
#include<stdio.h>
void main()
{
int a[20], n, loc, item,i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter location of deletion: ");
scanf("%d", &loc);
item = a[loc-1];
for(i=loc-1; i<n; i++)
{
a[i] = a[i+1];
}
n--;
printf("\nITEM deleted: %d", item);
printf("\n\nAfter deletion:\n");
for(i=0; i<n; i++)
{
printf("\n%d", a[i]);
}
getch();
}
Input –
Enter array size – 5
Enter array elements – 5 2 15 50 23
Enter Location of element to be deleted – 4
Output-
Array after deletion is – 5 2 15 23
- answered 8 years ago
- Sandy Hook
Your Answer