Example of using PHP to remove two-dimensional arrays

Source: Internet
Author: User
This article mainly introduces PHP's implementation of the two-dimensional array de-duplication function, which involves php's operations related to array traversal, judgment, and setting. it has some reference value, for more information about PHP, see the following example. We will share this with you for your reference. The details are as follows:

Deduplication of two-dimensional arrays in php. For example, the record queried from the database performs deduplication based on a key.

The code is as follows:

/*** Delete the data of the same item in a two-dimensional array (usually used for deduplication of the same record in the database query results) ** @ param array $ _ 2d_array two-dimensional array, similar: * $ tmpArr = array (* array ('id' => 1, 'value' => '15046f5de5bb708e '), * array ('id' => 1, 'value' => '15046f5de5bb708e '), *); * @ param string $ unique_key indicates the "id" key of the preceding array, or "value" key ** @ return mixed */function unique_2d_array_by_key ($ _ 2d_array, $ unique_key) {$ tmp_key [] = array (); foreach ($ _ 2d_array as $ key =>&$ item) {if (is_array ($ item) & isset ($ item [$ unique_key]) {if (in_array ($ item [$ unique_key], $ tmp_key) {unset ($ _ 2d_array [$ key]);} else {$ tmp_key [] = $ item [$ unique_key] ;}}return $ _ 2d_array;} // example: $ tmpArr = array ('id' => 1, 'value' => '15046f5de5bb708e '), array ('id' => 1, 'value' => '15046f5de5bb708e '),); print_r (@ unique_2d_array_by_key ($ tmpArr, id ));

Running result:

Array ( [0] => Array ( [id] => 1 [value] => 15046f5de5bb708e ) )

Principle: save the keys in the two-dimensional array to be de-duplicated and traverse and compare the next group of data. delete the keys if the key values are the same.

I hope this article will help you with PHP programming.

For more articles about how PHP implements the two-dimensional array de-duplication function example, refer to PHP Chinese network!

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.