Maison > développement back-end > tutoriel php > 深入学习PHP数组函数使用方法_PHP教程

深入学习PHP数组函数使用方法_PHP教程

WBOY
Libérer: 2016-07-15 13:27:13
original
732 Les gens l'ont consulté

PHP数组函数还是比较常用的,于是我研究了一下PHP数组函数,在这里拿出来和大家分享一下,希望对大家有用。今天我们学习PHP数组函数,数组是一种数据类型,它的使用频率相当高,学会处理数组会让你做网站时得心应手。

举个例子:你的网站数据库里存有大里的文章,现在你想在一个页面上显示20条娱乐新闻的标题,这20条标题从数据库里取出来后你想用20个变量表示吗?我想你不会这么笨吧,那么用什么变量能完全接收这20条数据呢?这就要用到数组变量。什么是数组呢?数组实际上是一个数据集合,相当于是一个数据容器,很多数据存放在里面,我们可以按一定方法存进去或取出来,还可以对它里面的数据进行排序等各种操作,还可以检查里面有没有我们想要的数据等等。

1.array_multisort()对多个数组或多维数组进行排序可以用来一次对多个数组进行排序或者根据某一维对多维数组进行排序。排序时保留原有的键名关联,实现的功能和SQL中的ORDERBY差不多。

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">ar1</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("10",100,100,"a");  </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">ar2</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(1,3,"2",1);  </span>
</li>
<li class="alt"><span>array_multisort($ar1,$ar2);  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
<li class="alt"><span>//返回结果(保持了原来数组的关联性)  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">ar1</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">Array</font></span><span>([0]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>10[1]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>a[2]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>100[3]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>100)  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">ar2</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">Array</font></span><span>([0]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>1[1]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>1[2]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>2[3]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>3)  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>//实现学生成绩排名  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">grade</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("score"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>array(70,95,70.0,60,"70"),  </span>
</li>
<li class="alt">
<span>"name"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>array("ZhangSan","LiSi","WangWu",  </span>
</li>
<li class=""><span>"ZhaoLiu","LiuQi"));  </span></li>
<li class="alt"><span>array_multisort($grade["score"],SORT_NUMERIC,SORT_DESC,  </span></li>
<li class=""><span>//将分数作为数值,由高到低排序  </span></li>
<li class="alt"><span>$grade["name"],SORT_STRING,SORT_ASC);  </span></li>
<li class=""><span>//将名字作为字符串,由小到大排序  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
<li class=""><span>//输出结果  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>array(  </span></li>
<li class="alt">
<span>'score'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span>
</li>
<li class=""><span>array(  </span></li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">0</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>95,  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">1</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'70',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">2</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>70,  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">3</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>70,  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">4</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>60,  </span>
</li>
<li class=""><span>),  </span></li>
<li class="alt">
<span>'name'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span>
</li>
<li class=""><span>array(  </span></li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">0</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'LiSi',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">1</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'LiuQi',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">2</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'WangWu',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">3</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'ZhangSan',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">4</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'ZhaoLiu',  </span>
</li>
<li class=""><span>),  </span></li>
<li class="alt"><span>)  </span></li>
</ol>
Copier après la connexion

2.array_rand()和shuffle(),一个是从数组中随机的取出一个或多个值,另一个是将数组中的值的顺序打乱,然后再可以从中取出值,它们都实现从数组中随机取出数据的功能,可以用于实现诸如抽奖之类的功能。

3.array_reduce()将回调函数function迭代地作用到input数组中的每一个单元中,从而将数组简化为单一的值。如果指定了可选参数initial,该参数将被当成是数组中的第一个值来处理,或者如果数组为空的话就作为最终返回值。

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li>
<li class=""><span>functionrsum($v,$w){  </span></li>
<li class="alt"><span>$v+=$w;  </span></li>
<li class=""><span>return$v;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>functionrmul($v,$w){  </span></li>
<li class=""><span>$v*=$w;  </span></li>
<li class="alt"><span>return$v;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">a</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(1,2,3,4,5);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">x</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>();  </span>
</li>
<li class=""><span>//实现array_sum()功能  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">b</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array_reduce</font></span><span>($a,"rsum");//</span><span class="attribute-value"><font color="#0000ff">1</font></span><span class="attribute"><font color="#ff0000">15</font></span><span>=1+2+3+4+5  </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">c</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array_reduce</font></span><span>($a,"rmul",10);//</span><span class="attribute-value"><font color="#0000ff">1</font></span><span class="attribute"><font color="#ff0000">1200</font></span><span>=1*2*3*4*5*10  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">d</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array_reduce</font></span><span>($x,"rsum",1);//1  </span>
</li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Copier après la connexion

这个函数可以实现求一个数组的和(积,差等功能)


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446540.htmlTechArticlePHP数组函数还是比较常用的,于是我研究了一下PHP数组函数,在这里拿出来和大家分享一下,希望对大家有用。今天我们学习PHP数组函数,...
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal