PHP two-dimensional array extraction function----eliminate unwanted data

Source: Internet
Author: User

Recently, the application interface found that the server returned to the app interface data is a two-dimensional array of forms, but this brings some problems, some data is useless, these useless data will cause trouble to the app, So the search solution found that the online and manual are not able to solve the problem of the function, so I wrote a function Delmembergetnewarray, can be two-dimensional array of redundant data to eliminate, for the future we deal with the data aspects of the convenience, see the code

1 /*2 * Delmembergetnewarray Get a new two-dimensional array3 * @ $data original array4 * @ $del _data var array incoming change factor5 * @ $flag bool is false is the original array delete the member containing the factor, true is to extract the member containing the factor6 */7 functionDelmembergetnewarray ($data,$del _data=NULL,$flag=false)8 {  9     if(Is_null($del _data))return false;Ten     $count=sizeof($del _data); One     $new _data=Array(); A     $org _count=sizeof($data[0]); -     if($org _count==$count)return false;  -     foreach($data  as $key=$value) the     { -     #extracting member actions for making -     if($flag= = ' true '){ -         if(!Is_array($del _data)){ +         #extract a single member action -             foreach($value  as $k=$v){ +                 if($k==$del _data){ A                 Continue; at}Else{ -                 unset($value[$k]); -             } -             }     -}Else{ -         foreach($value  as $k=$v){ in             foreach($del _data  as $del _key=$del _value){ -             if($k==$del _value){ to                 Continue;  +}Else{ -                 if(!In_array($k,$del _data)) the                 { *                     unset($value[$k]); $                 }Panax Notoginseng             } -             } the         }                 +         } A         $new _data[$key]=$value;  the}Else{ +     //incoming Array delete operation -     if(Is_array($del _data)) $     { $         foreach($del _data  as $del _value) -         { -                 unset($value[$del _value]);  the         } -     //passing in a single variable delete operationWuyi}Else{ the         unset($value[$del _data]); -     }     Wu     $new _data[$key]=$value; -     } About     } $     return $new _data; -}
Where $del _data can be a variable or an array, you can flexibly use the $flag parameter, such as a two-dimensional array of data a lot, you just want to extract the data you want, then you can take the data you want to extract (two-dimensional array member key name) as an array, such as $del_data= ( ' Name ', ' age '), passing in the function and set to Ture, generates a new two-dimensional array containing only the incoming array, and if the data of the two-dimensional array you have only one do not want, you can pass in a member that you want to delete (two-dimensional array member key name), and set to False is OK.

PHP two-dimensional array extraction function----eliminate unwanted data

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.