PHP implements two-dimensional array de-duplication function example, two-dimensional array example

Source: Internet
Author: User
Tags array example

PHP implements two-dimensional array de-duplication function example, two-dimensional array example

This example describes how PHP implements the two-dimensional array deduplication function. 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 them if the key values are the same.

PS: This site also has a simple and practical online text deduplication tool. We recommend it to you:

Online text deduplication tool:
Http://tools.jb51.net/aideddesign/txt_quchong

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.