• support@answerspoint.com

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 8 years ago
  • B Butts


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 */


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];
     printf("\nITEM deleted: %d", item);
     printf("\n\nAfter deletion:\n");
     for(i=0; i<n; i++)
              printf("\n%d", a[i]);


Input –
Enter array size – 5
Enter array elements – 5   2    15    50    23
Enter Location of element to be deleted – 4

Array after deletion is  – 5   2   15   23

  • answered 7 years ago
  • Sandy Hook

Your Answer

    Facebook Share        
  • asked 8 years ago
  • viewed 1819 times
  • active 8 years ago

Best Rated Questions