---
title: "php数据结构算法"
author: "piperzero"
date: "2013-06-13T18:19:00Z"
tags: []
slug: "php"
draft: false
lastmod: "2013-07-16T08:32:05Z"
id: 1906899
url: "https://piperzero.iteye.com/blog/1906899"
---

[&lt;?  
//--------------------  
// 基本数据结构算法  
//--------------------](http://www.verydemo.com/demo_c116_i3130.html)

[//二分查找（数组里查找某个元素）  
function bin\_sch($array, $low, $high, $k){  
if ($low &lt;= $high){  
$mid = intval(($low+$high)/2);  
if ($array\[$mid\] == $k){  
return $mid;  
}elseif ($k &lt; $array\[$mid\]){  
return bin\_sch($array, $low, $mid-1, $k);  
}else{  
return bin\_sch($array, $mid+1, $high, $k);  
}  
}  
return -1;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//顺序查找（数组里查找某个元素）  
function seq\_sch($array, $n, $k){  
$array\[$n\] = $k;  
for($i=0; $i&lt;$n; $i++){  
if($array\[$i\]==$k){  
break;  
}  
}  
if ($i&lt;$n){  
return $i;  
}else{  
return -1;  
}  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//线性表的删除（数组中实现）  
function delete\_array\_element($array, $i)  
{  
$len = count($array);  
for ($j=$i; $j&lt;$len; $j++){  
$array\[$j\] = $array\[$j+1\];  
}  
array\_pop($array);  
return $array;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//冒泡排序（数组排序）  
function bubble\_sort($array)  
{  
$count = count($array);  
if ($count &lt;= 0) return false;](http://www.verydemo.com/demo_c116_i3130.html)

[for($i=0; $i&lt;$count; $i++){  
for($j=$count-1; $j&gt;$i; $j--){  
if ($array\[$j\] &lt; $array\[$j-1\]){  
$tmp = $array\[$j\];  
$array\[$j\] = $array\[$j-1\];  
$array\[$j-1\] = $tmp;  
}  
}  
}  
return $array;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//快速排序（数组排序）  
function quick\_sort($array) {  
if (count($array) &lt;= 1) return $array;](http://www.verydemo.com/demo_c116_i3130.html)

[$key = $array\[0\];  
$left\_arr = array();  
$right\_arr = array();](http://www.verydemo.com/demo_c116_i3130.html)

[for ($i=1; $i&lt;count($array); $i++){  
if ($array\[$i\] &lt;= $key)  
$left\_arr\[\] = $array\[$i\];  
else  
$right\_arr\[\] = $array\[$i\];  
}](http://www.verydemo.com/demo_c116_i3130.html)

[$left\_arr = quick\_sort($left\_arr);  
$right\_arr = quick\_sort($right\_arr);](http://www.verydemo.com/demo_c116_i3130.html)

[return array\_merge($left\_arr, array($key), $right\_arr);  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//------------------------  
// PHP内置字符串函数实现  
//------------------------](http://www.verydemo.com/demo_c116_i3130.html)

[//字符串长度  
function strlen($str)  
{  
if ($str == '') return 0;](http://www.verydemo.com/demo_c116_i3130.html)

[$count = 0;  
while (1){  
if ($str\[$count\] != NULL){  
$count++;  
continue;  
}else{  
break;  
}  
}  
return $count;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//截取子串  
function substr($str, $start, $length=NULL)  
{  
if ($str=='' || $start&gt;strlen($str)) return;  
if (($length!=NULL) && ($start&gt;0) && ($length&gt;strlen($str)-$start)) return;  
if (($length!=NULL) && ($start&lt;0) && ($length&gt;strlen($str)+$start)) return;  
if ($length == NULL) $length = (strlen($str) - $start);  
\
if ($start &lt; 0){  
for ($i=(strlen($str)+$start); $i&lt;(strlen($str)+$start+$length); $i++) {  
$substr .= $str\[$i\];  
}  
}](http://www.verydemo.com/demo_c116_i3130.html)

[if ($length &gt; 0){  
for ($i=$start; $i&lt;($start+$length); $i++) {  
$substr .= $str\[$i\];  
}  
}](http://www.verydemo.com/demo_c116_i3130.html)

[if ($length &lt; 0){  
for ($i=$start; $i&lt;(strlen($str)+$length); $i++) {  
$substr .= $str\[$i\];  
}  
}  
return $substr;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//字符串翻转  
function strrev($str)  
{  
if ($str == '') return 0;  
for ($i=(strlen($str)-1); $i&gt;=0; $i--){  
$rev\_str .= $str\[$i\];  
}  
return $rev\_str;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//字符串比较  
function strcmp($s1, $s2)  
{  
if (strlen($s1) &lt; strlen($s2)) return -1;  
if (strlen($s1) &gt; strlen($s2)) return 1;](http://www.verydemo.com/demo_c116_i3130.html)

[for ($i=0; $i&lt;strlen($s1); $i++){  
if ($s1\[$i\] == $s2\[$i\]){  
continue;  
}else{  
return false;  
}  
}  
return 0;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//查找字符串  
function strstr($str, $substr)  
{  
$m = strlen($str);  
$n = strlen($substr);  
if ($m &lt; $n) return false;](http://www.verydemo.com/demo_c116_i3130.html)

[for ($i=0; $i&lt;=($m-$n+1); $i++){  
$sub = substr($str, $i, $n);  
if (strcmp($sub, $substr) == 0) return $i;  
}  
return false;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//字符串替换  
function str\_replace($substr, $newsubstr, $str)  
{  
$m = strlen($str);  
$n = strlen($substr);  
$x = strlen($newsubstr);  
if (strchr($str, $substr) == false) return false;](http://www.verydemo.com/demo_c116_i3130.html)

[for ($i=0; $i&lt;=($m-$n+1); $i++){  
$i = strchr($str, $substr);  
$str = str\_delete($str, $i, $n);  
$str = str\_insert($str, $i, $newstr);  
}  
return $str;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//--------------------  
// 自实现字符串处理函数  
//--------------------](http://www.verydemo.com/demo_c116_i3130.html)

[//插入一段字符串  
function str\_insert($str, $i, $substr)  
{  
for($j=0; $j&lt;$i; $j++){  
$startstr .= $str\[$j\];  
}  
for ($j=$i; $j&lt;strlen($str); $j++){  
$laststr .= $str\[$j\];  
}  
$str = ($startstr . $substr . $laststr);](http://www.verydemo.com/demo_c116_i3130.html)

[return $str;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//删除一段字符串  
function str\_delete($str, $i, $j)  
{  
for ($c=0; $c&lt;$i; $c++){  
$startstr .= $str\[$c\];  
}  
for ($c=($i+$j); $c&lt;strlen($str); $c++){  
$laststr .= $str\[$c\];  
}  
$str = ($startstr . $laststr);](http://www.verydemo.com/demo_c116_i3130.html)

[return $str;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//复制字符串  
function strcpy($s1, $s2)  
{  
if (strlen($s1)==NULL || !isset($s2)) return;](http://www.verydemo.com/demo_c116_i3130.html)

[for ($i=0; $i&lt;strlen($s1); $i++){  
$s2\[\] = $s1\[$i\];  
}  
return $s2;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//连接字符串  
function strcat($s1, $s2)  
{  
if (!isset($s1) || !isset($s2)) return;  
$newstr = $s1;  
for($i=0; $i&lt;count($s); $i++){  
$newstr .= $st\[$i\];  
}  
return $newsstr;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//简单编码函数（与php\_decode函数对应）  
function php\_encode($str)  
{  
if ($str=='' && strlen($str)&gt;128) return false;](http://www.verydemo.com/demo_c116_i3130.html)

[for($i=0; $i&lt;strlen($str); $i++){  
$c = ord($str\[$i\]);  
if ($c&gt;31 && $c&lt;107) $c += 20;  
if ($c&gt;106 && $c&lt;127) $c -= 75;  
$word = chr($c);  
$s .= $word;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[return $s;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//简单解码函数（与php\_encode函数对应）  
function php\_decode($str)  
{  
if ($str=='' && strlen($str)&gt;128) return false;](http://www.verydemo.com/demo_c116_i3130.html)

[for($i=0; $i&lt;strlen($str); $i++){  
$c = ord($word);  
if ($c&gt;106 && $c&lt;127) $c = $c-20;  
if ($c&gt;31 && $c&lt;107) $c = $c+75;  
$word = chr($c);  
$s .= $word;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[return $s;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//简单加密函数（与php\_decrypt函数对应）  
function php\_encrypt($str)  
{  
$encrypt\_key = 'abcdefghijklmnopqrstuvwxyz1234567890';  
$decrypt\_key = 'ngzqtcobmuhelkpdawxfyivrsj2468021359';](http://www.verydemo.com/demo_c116_i3130.html)

[if (strlen($str) == 0) return false;](http://www.verydemo.com/demo_c116_i3130.html)

[for ($i=0; $i&lt;strlen($str); $i++){  
for ($j=0; $j&lt;strlen($encrypt\_key); $j++){  
if ($str\[$i\] == $encrypt\_key\[$j\]){  
$enstr .= $decrypt\_key\[$j\];  
break;  
}  
}  
}](http://www.verydemo.com/demo_c116_i3130.html)

[return $enstr;  
}](http://www.verydemo.com/demo_c116_i3130.html)

[//简单解密函数（与php\_encrypt函数对应）  
function php\_decrypt($str)  
{  
$encrypt\_key = 'abcdefghijklmnopqrstuvwxyz1234567890';  
$decrypt\_key = 'ngzqtcobmuhelkpdawxfyivrsj2468021359';](http://www.verydemo.com/demo_c116_i3130.html)

[if (strlen($str) == 0) return false;](http://www.verydemo.com/demo_c116_i3130.html)

[for ($i=0; $i&lt;strlen($str); $i++){  
for ($j=0; $j&lt;strlen($decrypt\_key); $j++){  
if ($str\[$i\] == $decrypt\_key\[$j\]){  
$enstr .= $encrypt\_key\[$j\];  
break;  
}  
}  
}](http://www.verydemo.com/demo_c116_i3130.html)

[return $enstr;  
}](http://www.verydemo.com/demo_c116_i3130.html)

&lt;wbr&gt;&lt;/wbr&gt;