ensnare
ensnare

Reputation: 42093

Delete row from php array

How can I remove an element from an array?

For example:

$data = Array('first' , 'second' , 'third');
array_delete($data[2]);

#$data would now read Array('first', 'second')

Does such a built-in function exist? Thanks.

Upvotes: 21

Views: 54347

Answers (3)

alfred
alfred

Reputation: 1018

Use the unset method:

unset($data[2]);

Upvotes: 45

Angel.King.47
Angel.King.47

Reputation: 8004

The above answers work. But here is what i got from the site listed below. I think its cool.

//deletes a number on index $idx in array and returns the new array  
function array_delete($idx,$array) {  
    unset($array[$idx]);  
    return (is_array($array)) ? array_values($array) : null;  
}

http://dev.kafol.net/2009/02/php-array-delete.html

Upvotes: 4

dqhendricks
dqhendricks

Reputation: 19251

unset($data[2]);

yes it does. unset().

Upvotes: 4

Related Questions