网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

php定义数据结构,php定义数据结构是什么

PHP实现数据结构几个自定义PHP函数

####### 基本的数据结构 ########

创新互联专业为企业提供秦安网站建设、秦安做网站、秦安网站设计、秦安网站制作等企业网站建设、网页设计与制作、秦安企业网站模板建站服务,十多年秦安做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

//二分查找(数组里查找某个元素)

function bin_sch($array, $low, $high, $k){ 

if ($low = $high){ 

$mid = intval(($low+$high)/2); 

if ($array[$mid] == $k){ 

return $mid; 

}elseif ($k  $array[$mid]){ 

return bin_sch($array, $low, $mid-1, $k); 

}else{ 

return bin_sch($array, $mid+1, $high, $k); 

return -1; 

//线性表的删除(数组中实现)

function delete_array_element($array, $i)

{

$len = count($array); 

for ($j=$i; $j$len; $j++){

$array[$j] = $array[$j+1];

}

array_pop($array);

return $array;

}

//冒泡排序(数组排序)

function bubble_sort($array)

{

$count = count($array);

if ($count = 0) return false;

for($i=0; $i$count; $i++){

for($j=$count-1; $j$i; $j--){

if ($array[$j]  $array[$j-1]){

$tmp = $array[$j];

$array[$j] = $array[$j-1];

$array[$j-1] = $tmp;

}

}

}

return $array;

}

//快速排序(数组排序)

function quicksort($array) {

if (count($array) = 1) return $array;

$key = $array[0];

$left_arr = array();

$right_arr = array();

for ($i=1; $icount($array); $i++){

if ($array[$i] = $key)

$left_arr[] = $array[$i];

else

$right_arr[] = $array[$i];

}

$left_arr = quicksort($left_arr);

$right_arr = quicksort($right_arr);

return array_merge($left_arr, array($key), $right_arr);

}

####### 自实现函数 #######

//插入一段字符串

function str_insert($str, $i, $substr)

{

for($j=0; $j$i; $j++){

$startstr .= $str[$j];

}

for ($j=$i; $jstrlen($str); $j++){

$laststr .= $str[$j];

}

$str = ($startstr . $substr . $laststr);

return $str;

}

//删除一段字符串

function str_delete($str, $i, $j)

{

for ($c=0; $c$i; $c++){

$startstr .= $str[$c];

}

for ($c=($i+$j); $cstrlen($str); $c++){

$laststr .= $str[$c];

}

$str = ($startstr . $laststr);

return $str;

}

//复制字符串

function strcpy($s1, $s2)

{

if (strlen($s1)==NULL || !isset($s2)) return;

for ($i=0; $istrlen($s1); $i++){

$s2[] = $s1[$i];

}

return $s2;

}

//连接字符串

function strcat($s1, $s2)

{

if (!isset($s1) || !isset($s2)) return;

$newstr = $s1;

for($i=0; $icount($s); $i++){

$newstr .= $st[$i];

}

return $newsstr;

}

//简单编码函数(与php_decode函数对应)

function php_encode($str)

{

if ($str==''  strlen($str)128) return false;

for($i=0; $istrlen($str); $i++){

$c = ord($str[$i]);

if ($c31  $c107) $c += 20;

if ($c106  $c127) $c -= 75;

$word = chr($c);

$s .= $word;

return $s; 

}

//简单解码函数(与php_encode函数对应)

function php_decode($str)

{

if ($str==''  strlen($str)128) return false;

for($i=0; $istrlen($str); $i++){

$c = ord($word);

if ($c106  $c127) $c = $c-20;

if ($c31  $c107) $c = $c+75;

$word = chr($c);

$s .= $word;

return $s; 

}

//简单加密函数(与php_decrypt函数对应)

function php_encrypt($str)

{

$encrypt_key = 'abcdefghijklmnopqrstuvwxyz1234567890';

$decrypt_key = 'ngzqtcobmuhelkpdawxfyivrsj2468021359';

if (strlen($str) == 0) return false;

for ($i=0; $istrlen($str); $i++){

for ($j=0; $jstrlen($encrypt_key); $j++){

if ($str[$i] == $encrypt_key[$j]){

$enstr .= $decrypt_key[$j];

break;

}

}

}

return $enstr;

}

//简单解密函数(与php_encrypt函数对应)

function php_decrypt($str)

{

$encrypt_key = 'abcdefghijklmnopqrstuvwxyz1234567890';

$decrypt_key = 'ngzqtcobmuhelkpdawxfyivrsj2468021359';

if (strlen($str) == 0) return false;

for ($i=0; $istrlen($str); $i++){

for ($j=0; $jstrlen($decrypt_key); $j++){

if ($str[$i] == $decrypt_key[$j]){

$enstr .= $encrypt_key[$j];

break;

}

}

}

return $enstr;

到此为止吧:)

php数据结构与算法(PHP描述) 快速排序 quick sort

复制代码

代码如下:

?php

/**

*

快速排序

quick

sort

*

**/

function

sort_quick($arrData)

{

if(empty($arrData)

||

!is_array($arrData))

return

false;

$flag

=

$arrData[0];

$len

=

count($arrData)

-

1;

if($len

==

0)

return

$arrData;

//

如果只有一个数据的数组直接返回

$arrLeft

=

array();

$arrRight

=

array();

$len_l

=

0;

$len_r

=

0;

for($i

=

1;

$i

=

$len;$i++)

{

if($arrData[$i]

$flag)

{

$arrLeft[$len_l]

=

$arrData[$i];

//

小于的放左边

$len_l++;

}

else

{

$arrRight[$len_r]

=

$arrData[$i];

//

大于等于的放右边

$len_r++;

}

}

//

合并数组

$arrResult

=

array();

if($len_l)

{

$arrLeft

=

sort_quick($arrLeft);

for($i

=

0;$i

=

$len_l

-

1;

$i++

)

{

$arrResult[$i]

=

$arrLeft[$i];

}

}

$arrResult[$len_l]

=

$flag;

$len_l++;

if($len_r)

{

$arrRight

=

sort_quick($arrRight);

for($i

=

0;$i

=

$len_r

-

1;

$i++

)

{

$arrResult[$len_l]

=

$arrRight[$i];

$len_l++;

}

}

echo

"==

",$flag,"

==========================================br/";

echo

"data

:

",print_r($arrData),"br/";

echo

"filter

left:

",print_r($arrLeft),"br/";

echo

"filter

right:

",print_r($arrRight),"br/";

echo

"return

:

",print_r($arrResult),"br/";

return

$arrResult;

}

//$list

=

array(4,3,2,1,5,7,3,7);

$list

=

array(4,51,6,73,2,5,9,33,50,3,4,6,1,4,67);

$list

=

sort_quick($list);

echo

"pre";print_r($list);

php 数据库中输出的数据结构

从数据库读出来的原始数据是资源。还不是数组。

$result = mysql_Query("select * from tb_admin where parid=1")

while($list = mysql_fetch_array($result)){

print_r($list);//这里输出的数组是将原数组拆开来输出。

}

原始的数据结构应是:

array(

[0]=array(

[id]=01

[classname]=我是

[url]=baidu.com

)

[1]=array(

//这里同上,不两累赘

)

)

PHP中数据类型 array和object有什么区别?

具体的概念区别你可以查看php帮助手册,我说下个人的理解:

通常来说,array是数组,而object是对象,两者有很大的区别,最主要的区别我觉得对象一般要定义行为,其目的是为了封装,而数组主要定义数据结构。

但这两种在php语言里区别比较模糊,因为php是支持范数据类型,所以array可以是object,反之亦然。

php数据分析

这个问题其实简单,你可以在foreach外先设置一个空数组,作用是把2016-11-29作为键 出现的次数作为value存储起来, 代码

$tmpArr = array();

foreach($ips as $key = $value)

{

$tmpArr[explode('_',$key)[0]] ++;

}

foreach($tmpArr as $key = $tmp)

{

echo $key." ".$tmp."br";

}

但是具体的数据结构要根据你自己情况来定。

PHP 数据结构 算法 三元组 Triplet

复制代码

代码如下:

?php

/**

*

三元组

Triplet

*

*/

class

Triplet

{

private

$_data

=

null;

//

初始化三元组

public

function

init($val1,$val2,$val3)

{

$this-_data[0]

=

$val1;

$this-_data[1]

=

$val2;

$this-_data[2]

=

$val3;

return

true;

}

//

销毁三元组

public

function

destroy()

{

unset($this-_data);

return

true;

}

//

返回第$key的值

public

function

get($key)

{

if($key

1

||

$key

3)

return

false;

return

$this-_data[$key

-

1];

}

//

设置第$key元的值为$val

public

function

put($key,$val)

{

if($key

1

||

$key

3)

return

false;

$this-_data[$key

-

1]

=

$val;

return

true;

}

//

是否按升序排序

public

function

isAscending()

{

return

($this-_data[0]

=

$this-_data[1])

($this-_data[1]

=

$this-_data[2]);

}

//

是否按降序排序

public

function

isDescending()

{

return

($this-_data[0]

=

$this-_data[1])

($this-_data[1]

=

$this-_data[2]);

}

//

获取最大值

public

function

max()

{

return

($this-_data[0]

=

$this-_data[1])?

($this-_data[0]

=

$this-_data[2])?

$this-_data[0]

:

$this-_data[2]

:

($this-_data[1]

=

$this-_data[2])?

$this-_data[1]

:

$this-_data[2];

}

//

获取最小值

public

function

min()

{

return

($this-_data[0]

=

$this-_data[1])?

($this-_data[0]

=

$this-_data[2])?

$this-_data[0]

:

$this-_data[2]

:

($this-_data[1]

=

$this-_data[2])?

$this-_data[1]

:

$this-_data[2];

}

}

//

$objTriplet

=

new

Triplet();

echo

"init:";var_dump($objTriplet-init(1,2,3));

echo

"br/";

echo

"get

1:";var_dump($objTriplet-get(1));

echo

"br/";

echo

"get

4:";var_dump($objTriplet-get(4));

echo

"br/";

//

false

echo

"put

3,4:";var_dump($objTriplet-put(3,4));

echo

"br/";

echo

"max:";var_dump($objTriplet-max());

echo

"br/";

echo

"min:";var_dump($objTriplet-min());

echo

"br/";

echo

"isAscending:";var_dump($objTriplet-isAscending());

echo

"br/";

echo

"isDescending:";var_dump($objTriplet-isDescending());

echo

"br/";

?


名称栏目:php定义数据结构,php定义数据结构是什么
本文来源:http://bjjierui.cn/article/hdpdoc.html

其他资讯