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!