Home >php教程 >php手册 >详谈PHP数组

详谈PHP数组

WBOY
WBOYOriginal
2016-06-13 11:04:53909browse

随着时代的发展,我们的编程技术也越来越多,对于PHP想必大家都很了解吧,我们这里就介绍一下PHP数组吧。PHP数组是最常见的批量数据存储结构,由于数组采用顺序存储,所以可以方便的进行随机存储。在很多语言中,数组都是静态的,必须在使用前指定数组的长度,而在PHP中,你完全不用操心这个问题,数组是完全动态的。你甚至可以这样来使用一个数组。

<ol class="dp-xml">
<li class="alt"><span><span>PHP CODE  </span></span></li>
<li class=""><span>// DEMO of Array  </span></li>
<li class="alt"><span>demo_array[] = 1;  </span></li>
<li class=""><span>demo_array[] = 2; </span></li>
</ol>

下面我们用PHP数组来模拟一下中国广大的劳动人民很喜欢的一种体育运动——麻将的洗牌。这里先普及一下麻将的规则,以后伺候MM的母亲时有用的:)。首先,麻将是要4个人玩的,3P是不行的。总共108张牌,摸完牌后庄家14张,另外3人每人13张,还剩55张。(麻将全国有N种玩法,这里讲的是Easy家乡的玩法,没有中发白等花牌,是Easy见过的玩法中最简单的了。)然后我们来分析一下如何用随机数来模拟这个过程,这样的练习不妨多做,因为从具体事例中提取出数学模型是编程的基本功。首先,我们用一个长度为108的数组来存放这108张牌,初始化的时候不妨假设它们是顺序排放的。

然后,我们来模拟洗牌的过程,这实际上是若干次的两张牌交换的过程。我们产生两个在1~108之间的随机数,然后交换以这两个数为index的数组元素的值就可以了。因为要多次交换,我们可以把这个过程定义为一个函数。接着,就是把这108张牌分给4个人,实际上,只要前边的交换次数足够大,直接切成5份就OK了。不过为了更好的模拟现实和表现数组的特性,我们采用循环的方式来分发。就是每个人一次四张,然后一个人一张,庄家两张。好了,整个过程就是这样的了。下边我们通过程序来说明一些更为细节的东西。

<ol class="dp-xml">
<li class="alt"><span><span>PHP CODE  </span></span></li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span>
</li>
<li class="alt"><span>/** ************************************************************  </span></li>
<li class=""><span>* 一个使用数组来模拟麻将洗牌和发牌过程的例子  </span></li>
<li class="alt"><span>* @ Easy@bjpeu.edu.cn  </span></li>
<li class=""><span>* @ 2003-6-3  </span></li>
<li class="alt"><span>** *************************************************************/  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>// 换牌函数  </span></li>
<li class=""><span>function swap()  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>// 将$majiangArray定义为全局变量  </span></li>
<li class="alt"><span>// 这样可以在函数中进行操作  </span></li>
<li class=""><span>global $majiangArray;  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>// 产生两个1到108的随机数  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">index1</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">rand</font></span><span>( 1 , 108 );  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">index2</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">rand</font></span><span>( 1 , 108 );  </span>
</li>
<li class="alt"><span> </span></li>
<li class=""><span>// 交换相应的数组元素的值  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">tmp</font></span><span> = $majiangArray[ $index1 ];  </span>
</li>
<li class=""><span>$majiangArray[ $index1 ] = $majiangArray[ $index2 ];  </span></li>
<li class="alt"><span>$majiangArray[ $index2 ] = $tmp;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>// 将数组的内容用相应的图片表示出来的函数  </span></li>
<li class="alt"><span>function showArray( $arrayName )  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">count</font></span><span class="attribute-value"><font color="#0000ff">count</font></span><span> = count ( $arrayName );  </span>
</li>
<li class="">
<span>for ( $</span><span class="attribute"><font color="#ff0000">i</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span> ; $i </span><span class="tag"><strong><font color="#006699"></font></strong></span><span> $count ; $i++ )  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>echo '</span><strong><font color="#006699"><span class="tag"><span class="tag-name">img</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">src</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"img/'. $arrayName[ $i ] .'"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span> ';//显示相应的图片  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class="">
<span>echo '</span><strong><font color="#006699"><span class="tag"><span class="tag-name">p</span><span class="tag">></span></span></font></strong><span>';//换行  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>// 主程序部分开始  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>// 初始化数组  </span></li>
<li class=""><span>unset( $majiangArray );  </span></li>
<li class="alt"><span>$majiangArray[] = '0'; // 数据从1开始存放,跳过0  </span></li>
<li class="">
<span>for ( $</span><span class="attribute"><font color="#ff0000">i</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">1</font></span><span> ; $i </span><span class="tag"><strong><font color="#006699"></font></strong></span><span>= 3 ; $i++ )// 共有三种花色  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>for ( $</span><span class="attribute"><font color="#ff0000">j</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">1</font></span><span> ; $j </span><span class="tag"><strong><font color="#006699"></font></strong></span><span>= 9 ; $j++ )// 一个花色中有九种牌  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>for ( $</span><span class="attribute"><font color="#ff0000">k</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">1</font></span><span> ; $k </span><span class="tag"><strong><font color="#006699"></font></strong></span><span>= 4 ; $k++ )// 每种牌有四张  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span>$majiangArray[] = $i . '_' . $j . '.gif';  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>// 开始洗牌  </span></li>
<li class="">
<span>for ( $</span><span class="attribute"><font color="#ff0000">i</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">1</font></span><span> ; $i </span><span class="tag"><strong><font color="#006699"></font></strong></span><span>= 1000 ; $i ++ )// 循环变量i被重新赋值重用  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span>swap();  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>// 初始化四个用户  </span></li>
<li class=""><span>unset( $user1 );  </span></li>
<li class="alt"><span>unset( $user2 );  </span></li>
<li class=""><span>unset( $user3 );  </span></li>
<li class="alt"><span>unset( $user4 );  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>// $seek是进度指针,纪录当前应该从那里发牌  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">seek</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">1</font></span><span>;  </span>
</li>
<li class="alt"><span> </span></li>
<li class="">
<span>for ( $</span><span class="attribute"><font color="#ff0000">i</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">1</font></span><span> ; $i </span><span class="tag"><strong><font color="#006699"></font></strong></span><span>= 3 ; $i++ )// 总共摸三次四张的  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>for ( $</span><span class="attribute"><font color="#ff0000">j</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">1</font></span><span> ; $j </span><span class="tag"><strong><font color="#006699"></font></strong></span><span>= 4 ; $j++ )// 四个人  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>for ( $</span><span class="attribute"><font color="#ff0000">k</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">1</font></span><span> ; $k </span><span class="tag"><strong><font color="#006699"></font></strong></span><span>= 4 ; $k++ )// 一次四张  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">name</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'user'</font></span><span>.$j;  </span>
</li>
<li class="alt"><span>${$name}[] = $majiangArray[ $seek++ ];  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>// 然后一人一张  </span></li>
<li class="alt">
<span>for ( $</span><span class="attribute"><font color="#ff0000">i</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">1</font></span><span> ; $i </span><span class="tag"><strong><font color="#006699"></font></strong></span><span>= 4 ; $i++ )  </span>
</li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">name</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'user'</font></span><span>.$i;  </span>
</li>
<li class=""><span>${$name}[] = $majiangArray[ $seek++ ];  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>// 最后庄家多摸一张  </span></li>
<li class=""><span>$user4[] = $majiangArray[ $seek ];  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>// 分牌结束 显示结果  </span></li>
<li class="alt"><span>showArray( $user1 );  </span></li>
<li class=""><span>showArray( $user2 );  </span></li>
<li class="alt"><span>showArray( $user3 );  </span></li>
<li class=""><span>showArray( $user4 );  </span></li>
<li class="alt"><span> </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn