//1.冒泡排序
function bubble_sort($arr)
{
$len = count($arr);
if ($len <= 1) return $arr;
$mid = 0;
for ($i = 0; $i < $len - 1; $i++) {
for ($j = 0; $j < $len - 1 - $i; $j++) {
if ($arr[$j] > $arr[$j + 1]) {
$mid = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $mid;
}
}
}
return $arr;
}
echo '1.冒泡排序:';
print_r(bubble_sort($arr));
echo '<br/>';
//2.快速排序
function quick_sort($arr)
{
if (count($arr) <= 1) return $arr;
$key = $arr[0];
$left_arr = [];
$right_arr = [];
for ($i = 1; $i < count($arr); $i++) {
if ($arr[$i] < $key) {
$left_arr[] = $arr[$i];
} else {
$right_arr[] = $arr[$i];
}
}
$left_arr = quick_sort($left_arr);
$right_arr = quick_sort($right_arr);
return array_merge($left_arr, array($key), $right_arr);
}
echo '2.快速排序:';
print_r(quick_sort($arr));
echo '<br/>';
//3.二分查找 !!已排序(递归、循环) 返回key
function binary_search($arr, $target, $start, $end)
{
$midd = floor(($start + $end) / 2);
if ($arr[$midd] == $target) return $midd;
if ($end > $start) {
if ($arr[$midd] > $target) {
return binary_search($arr, $target, 0, $midd - 1);
} else {
return binary_search($arr, $target, $midd + 1, $end);
}
}
return false;
}
echo '3.二分查找:';
$arr1 = sort($arr);//函数返回bool
var_dump(binary_search($arr, 17,0,count($arr)-1));
echo '<br/>';
//4.检查ip是否正确
function checkIp($ip)
{
$arr = explode(',', $ip);
if (count($arr) == 4 && $arr[0] != 0) {
for ($i = 0; $i < 4; $i++) {
if ($arr[$i] < '0' && $arr[$i] > '255') {
return 0;
}else{
return 1;
}
}
} else {
return 0;
}
}
echo checkIp('555,1,215,12');
echo '<br/>';
//5.两有序数组合并成一个有序数组(去重)
$arrA = array(1,2,3,4,5,6,7,8);
$arrB = array(3,4,5,7,9,10,11,13);
//方法1
function mergeOrderly1($arrA, $arrB)
{
$i = 0;
$j = 0;
$int = array();
while ($i < count($arrA) && $j < count($arrB)) {
$int[] = $arrA[$i] < $arrB[$j] ? $arrA[$i++] : $arrB[$j++];
}
while ($i < count($arrA)) {
$int[] = $arrA[$i++];
}
while ($j < count($arrB)) {
$int[] = $arrB[$j++];
}
$int = array_unique($int);//$int = array_flip(array_flip($int));
return $int;
}
print_r(mergeOrderly1($arrA, $arrB));*/
//6、单例 (三私一公)
class single{
private function __construct()
{
}
private function __clone()
{
}
private static $instance;
public static function getinstance(){
if(!self::$instance instanceof single){
self::$instance = new self();
}
return self::$instance;
}
}
//$a = single::instance();
//$b = single::instance();
//$c = single::instance();
var_dump(single::getinstance());echo "<br/>";
var_dump(single::getinstance());echo "<br/>";
var_dump(single::getinstance());echo "<br/>";
//7、遍历文件夹及以下文件
function my_dir($dir) {
$files = array();
if(@$handle = opendir($dir)) { //注意这里要加一个@,不然会有warning错误提示:)
while(($file = readdir($handle)) !== false) {
if($file != ".." && $file != ".") { //排除根目录;
if(is_dir($dir."/".$file)) { //如果是子文件夹,就进行递归
$files[$file] = my_dir($dir."/".$file);
} else { //不然就将文件的名字存入数组;
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}
echo "<pre>";
print_r(my_dir("."));
echo "</pre>";*/
//8、向数组添加元素
$arr = [1,2,3,4,5,6,7,8,9];
array_push($arr,98,99,100); //栈尾添加
array_unshift($arr,200,201); //队首添加
echo '<pre>';
print_r($arr);
echo '</pre>';
//9、引用 foreach里的坑
$arr2 = ['a','b','c'];
foreach($arr2 as &$v){
echo $v."\n";
} // a b c
foreach($arr2 as $k=>$v){
echo $v."\n";
} // a b b
$data = ['a','b','c','d'];
foreach($data as $key => $value){
$value = &$data[$key];
}
print_r($data); // b c d d
//10、面向对象
class A{
protected $a = 1;
function x(){
echo $this->a++;
}
}
$a = new A();
$b = $a;
$c = new A();
$b->x(); //1
$a->x(); //2
$c->x(); //1
$b = $c;
$b->x(); //2
$a->x(); //3
class SomeClass { private $properties = []; public function __set($name, $value) { $this->properties[$name] = $value; } public function __get($name) { return $this->properties[$name]; } } $obj = new SomeClass(); $obj->name = 'chang'; $obj->age = 20; $obj->gender = 'male'; var_dump($obj->name); //string(5) "chang" var_dump(isset($obj->name)); //bool(false)