How to remove null records from multi-dimensional arrays (a bug was fixed last time.

Source: Internet
Author: User
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]);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.