Home >Backend Development >PHP Tutorial >PHP bubble sort, quick sort, quick search, two-dimensional array deduplication example sharing_PHP tutorial
1. Bubble sort
function quick_sort($array)
Copy code
/**
* Quickly find the position where the value first appears
* @param array $array Array
* @param string $k The value to be found
* @param int $low The smallest key of the search range Value
* @param int $high The maximum key value of the range
*/
function search($array, $k, $low=0, $high=0)
{
// Determine whether it is the first call.
if(count($array)!=0 and $high == 0){
$high = count($array);
}
//If there are remaining array elements
if($low <= $high){
//Take the middle value of $low and $high
$mid = intval(($low+$high)/2);
//If If found, return
if ($array[$mid] == $k){
return $mid;
}
//If not found, continue to search
elseif ($k " k, $mid+1, $high);
}
}
return -1;
}
$array = array(4,5,7,8,9,10, ;
4. Remove duplicate items from the two-dimensional array
Copy the code
$temp = array_unique($temp); //Remove duplicate strings, that is, repeated one-dimensional arrays
foreach ($temp as $k => $v){
//$ temp[$k] = explode(",",$v); //Reassemble the disassembled array
$temp[$k]= array_combine($keyArray ,explode(",",trim($ v)));
}
return $temp;
}
$testArray=array_unique_fb(array(array('a'=>1,'b'=>2,'c'=>3),
array('a'=>1, 'b'=>2,'c'=>3),array('a'=>1,'b'=>2,'c'=>3)),array('a' ,'b','c''));
print_r($testArray);