How to delete null records in multi-dimensional arrays (This post was last fixed by bug A9925 at 2015-03-1618: 29: 58. edit the original post: bbs. csdn. nettopics390997712 latest question: when there is a value of & nbsp; '0' & nbsp; in the array, this element is deleted, in fact, this is a value-oriented multi-dimensional array to delete null records (the last bug solved
This post was last edited by A9925 on 18:29:58
Original Post
: Http://bbs.csdn.net/topics/390997712
In the latest question, when there is a value of '0' in the array, this element will be deleted, but in fact this is a value element.
["Status"] =>
String (1) "0"
$ A = array (
Array ('a' => 0, 'B' => ''),
Array ('a' => 0, 'B' => ''),
Array ('a' => 1, 'B' => '2 '),
);
Print_r (array_values (array_no_empty ($ )));
Function array_no_empty ($ arr ){
If (is_array ($ arr )){
Foreach ($ arr as $ k =>$ v ){
If (empty ($ v) unset ($ arr [$ k]);
Elseif (is_array ($ v )){
$ T = array_no_empty ($ v );
If ($ t) $ arr [$ k] = $ t;
Else unset ($ arr [$ k]);
}
}
}
Return $ arr;
}
How can this method be improved? thank you!
------ Solution ----------------------
If (empty () is changed to if ($ v = false) {unset ..........}
------ Solution ----------------------
If (empty ($ v) unset ($ arr [$ k]);
Change
If (empty ($ v) & $ v! = 0) unset ($ arr [$ k]);