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