Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i delete a item at index X in array? [duplicate]

Tags:

Possible Duplicate:
How to delete an element from an array in php?

I have a list of things, cars for instance

$cars[0] = "audi";
$cars[1] = "saab";
$cars[2] = "volvo";
$cars[3] = "vw";

How do i delete "volvo" from the list?

like image 914
Jason94 Avatar asked Feb 01 '11 08:02

Jason94


People also ask

How do I remove a repeated value from an array?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.

How do I remove an item from an array index?

pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.

How do you find the index of duplicate elements in an array?

indexOf() function. The idea is to compare the index of all items in an array with an index of their first occurrence. If both indices don't match for any item in the array, you can say that the current item is duplicated. To return a new array with duplicates, use the filter() method.


2 Answers

$volvoIndex = array_search('volvo', $cars);
unset($cars[$volvoIndex]);
like image 72
deceze Avatar answered Oct 04 '22 18:10

deceze


you can do with unset

unset($cars[2]);

But after that you need to iterate array with foreach

like image 37
Shakti Singh Avatar answered Oct 04 '22 18:10

Shakti Singh