This problem is mainly to examine the bottom realization of array_unqiue
PHP in Array_unique source code is:
Php_function (Array_unique)4{5//Defining variables6 Zval *array, *tmp7 Buckets *P8structBucketindex {9 Buckets *bTen unsignedIntI11};12struct Bucketindex *artmp, *cmpdata, *lastkept;UnsignedIntI14Long Sort_type =php_sort_string;1516//Parsing parameters17if (Zend_parse_parameters (Zend_num_args () TSRMLS_CC,"A|l", &array, &sort_type) = =FAILURE) {18Return;19}2021st//Setting the comparison function22Php_set_compare_func (Sort_type tsrmls_cc);2324//Initialize the returned array25Array_init_size (Return_value, zend_hash_num_elements (z_arrval_p (array)));26//Copy a value to a new arrayZend_hash_copy (Z_arrval_p (Return_value), z_arrval_p (array), (copy_ctor_func_t) Zval_add_ref, (void *) &tmp,sizeof (zval*));2829if (z_arrval_p (array)->nnumofelements <=1) {/*Don't do anything.*/30Return;31}3233/*Creates an array and sorts according to the pointer of Target_hash buckets*/Artmp = (struct Bucketindex *) Pemalloc ((z_arrval_p (array)->nnumofelements +1) *sizeofstruct bucketindex), z_arrval_p (array),Persistent);35if (!ARTMP) {36Zval_dtor (Return_value);37Return_false;38}39for (i =0, p = z_arrval_p (array)->plisthead; P i++, p = p->Plistnext) {ARTMP[I].B =PARTMP[I].I =I42}ARTMP[I].B =NULL;44//SortZend_qsort ((void *) artmp, I,sizeofstructBucketindex), Php_array_data_compare tsrmls_cc);4647/*Iterate through the sorted array, then delete the duplicate elements*/Lastkept =artmp;49for (Cmpdata = artmp +1; cmpdata->b; cmpdata++) {50If(Php_array_data_compare (lastkept, Cmpdata tsrmls_cc)) {Wuyi lastkept =Cmpdata;52}Else{53if (Lastkept->i > cmpdata->i) {Si p = lastkept->bLastkept =Cmpdata;56}Else{p = cmpdata->b58}59if (p->nkeylength = =0) {Zend_hash_index_del (Z_arrval_p (return_value), p->h);61}Else{62if (z_arrval_p (return_value) = = &EG (symbol_table)) {63 zend_delete_global_variable (P->arkey, p->nkeylength-1 TSRMLS_CC); 64} else {65 Zend_hash_quick_del (z_arrval_p (Return_value), P->arkey, P->nkeylength, p-> h); 66 }67 }68 }69 }70 pefree (artmp, z_arrval_p (array)->persistent) ; 71 "
############################################################################################################### ##########################################
Also want to dig deep to recommend a few good articles to you:
http://blog.csdn.net/lz610756247/article/details/51512918
PHP Source Comments: HTTPS://GITHUB.COM/HOOHACK/READ-PHP-SRC
#####################################################################################################
Use PHP to implement the bottom of the function.
Ideas:
1. Sort the array first.
First step: Sort the array first, using bubble sort
function Bubble ($arr)
{
$len = count ($arr); Count the number of array elements
for ($i =1; $i < $len; $i + +)//number of comparisons
{
for ($j = 0; $j < $len-$j; $j + +)//number of cycles
{
if ($arr [$i] > $arr [$j])
{
$temp = $arr [$i];
$arr [$i] = $arr [$j];
$arr [$j] = $temp;
}
}
}
return $arr;
}
Main functions function DD ($ARR)
{
$data = Bubble ($arr);
for ($i = 0; $i < count ($data); $i + +)
{
if ($data [$i] = = $data [$i +1])
{
Unset ($data [$i +1]);
}
}
return $arr;
}
$arr = [2,3,42,1,23,2,4,55,73,5];
DD ($arr);
######
Add: Running the above code will package a notice error, with error_reporting (e_all| | E_notice) Mask error is all that is possible.
The main reason is that the $data array is an index array, after unset, the key will appear vacant parts, so error, in fact, not an error, is a "reminder" of PHP.
De-duplication of repeated elements of an array with code-interview study Questions