网站首页 > 文章中心 > 其它

php多个数据结构

作者:小编 更新时间:2023-09-25 18:56:12 浏览量:46人看过

PHP 多个数组的排列组合

$d = array($a,$b,$c);

//计算每一个数组的长度

$len = 1;

$arrLen = count($d); //需要排列数组有多少个

$recIndex = null; //记录当前该取的位置

php多个数据结构-图1

//foreach 计数

foreach ($d as $key = $value) {

$recIndex[] = 0;//第一次全部取第0个

}

//算出% 的值

$count = 1;

php多个数据结构-图2

foreach ($lenRec as $key = $value) {

$moduloVal = 1;

if($arrLen == $count){

$modulo[] = count($d[$arrLen-1]); //等于最后一个的长度

}else{

$count_1 = 1;

foreach ($lenRec as $index = $item) {

$count_1 $count $moduloVal = $moduloVal*$item;

$count_1 ++;

$modulo[] = $moduloVal;

$count ++;//为了防止$d key是有值的 不是自然序列 需要计数

$i = 1;

while ( $i = $len) {

$html = '';

$temp = '';

foreach ($d as $value) {

echo $html."br";

foreach ($modulo as $key = $value) {

if($i%$value == 0 $key $arrLen - 1 ){

$recIndex[$key] = $recIndex[$key] +1;

if($key == $arrLen - 1){

if($i%$value == 0){

$recIndex[$key] = 0;

$i ++;

//改变获取的位置

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

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

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

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

if?($low?=?$high){?

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;?

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

php多个数据结构-图3

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;

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

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);

//复制字符串

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

//连接字符串

$newstr?=?$s1;

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

$newstr?.=?$st[$i];

return?$newsstr;

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

function?php_encode($str)

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

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

$word?=?chr($c);

$s?.=?$word;

return?$s;?

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

function?php_decode($str)

$c?=?ord($word);

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

function?php_encrypt($str)

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)

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

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

$enstr?.=?$encrypt_key[$j];

到此为止吧:)

PHP 数据结构队列(SplQueue)和优先队列(SplPriorityQueue)简单使用实例

队列这种数据结构更简单,就像我们生活中排队一样,它的特性是先进先出(FIFO).

PHP

SPL中SplQueue类就是实现队列操作,和栈一样,它也可以继承双链表(SplDoublyLinkedList)轻松实现.

SplQueue类摘要如下:

SplQueue简单使用如下:

复制代码

代码如下:

$queue

=

new

SplQueue();

/**

*

可见队列和双链表的区别就是IteratorMode改变了而已,栈的IteratorMode只能为:

(1)SplDoublyLinkedList::IT_MODE_FIFO

|

SplDoublyLinkedList::IT_MODE_KEEP

(默认值,迭代后数据保存)

SplDoublyLinkedList::IT_MODE_DELETE

(迭代后数据删除)

*/

$queue-setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO

SplDoublyLinkedList::IT_MODE_DELETE);

//SplQueue::enqueue()其实就是

SplDoublyLinkedList::push()

$queue-enqueue('a');

$queue-enqueue('b');

$queue-enqueue('c');

//SplQueue::dequeue()其实就是

SplDoublyLinkedList::shift()

print_r($queue-dequeue());

foreach($queue

as

$item)

echo

$item

.

PHP_EOL;

print_r($queue);

而优先队列SplPriorityQueue是基于堆(后文介绍)实现的.

SplPriorityQueue的类摘要如下:

SplPriorityQueue简单使用:

$pq

SplPriorityQueue();

$pq-insert('a',

①.0);

$pq-insert('b',

①.);

$pq-insert('c',

$pq-count()

.PHP_EOL;

$pq-current()

//a

设置元素出队模式

SplPriorityQueue::EXTR_DATA

仅提取值

SplPriorityQueue::EXTR_PRIORITY

仅提取优先级

SplPriorityQueue::EXTR_BOTH

提取数组包含值和优先级

$pq-setExtractFlags(SplPriorityQueue::EXTR_DATA);

while($pq-valid())

print_r($pq-current());

c

b

$pq-next();

php 数组能实现哪些数据结构

数组就是典型的数据结构了,使用数组操作函数,就可以实现单向和多向队列了. 操作函数有: array_shift array_unshift array_push array_pop

PHP有哪些数据结构可以存数据

数据库索引文件采用数据结构概述: 1、非主键索引需要在数据表本身的存储空间外额外开销存储空间,所以在更新的时候可能不仅要更新数据表本身,还要更新非主键索引,更新内容更多了,所以导致速度降低

以上就是土嘎嘎小编为大家整理的php多个数据结构相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章