Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete row from php array

Tags:

arrays

php

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.

like image 507
ensnare Avatar asked Jan 18 '11 17:01

ensnare


2 Answers

yes. i would have made it shorter, but need at least 30- charcters. so here you go:

unset($data[2]);
like image 158
alfred Avatar answered Sep 28 '22 10:09

alfred


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

like image 45
Angel.King.47 Avatar answered Sep 28 '22 10:09

Angel.King.47