随着php的快速发展,用它的人越来越多,在php数组学习摘录部分了解到最基本的php数组的建立和数组元素的显示。需要深入学习下php数组的相关操作。首先接触的就是php数组排序、降序的排序问题。
快速创建数组的函数range()
<OL class=dp-xml><LI class=alt><SPAN><SPAN>比如range()函数可以快速创建从1到9的数字数组: </SPAN></SPAN><LI class=""><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>numbers</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>range</FONT></SPAN><SPAN>(1,9); </SPAN></SPAN><LI class=""><SPAN>echo $numbers[1]; </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN> </SPAN></SPAN></LI></OL>
当然,使用range(9,1)则创建了9到1的数字数组。同时,range()还可以创建从a到z 的字符数组:
<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 class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>numbers</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>range</FONT></SPAN><SPAN>(a,z); </SPAN></SPAN><LI class=alt><SPAN>foreach ($numbers as $mychrs) </SPAN><LI class=""><SPAN>echo $mychrs." "; </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN> </SPAN></SPAN></LI></OL>
<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 class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>people</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('name','sex','nation','birth'); </SPAN></SPAN><LI class=alt><SPAN>foreach ($people as $mychrs) </SPAN><LI class=""><SPAN>echo $mychrs." "; </SPAN><LI class=alt><SPAN>sort($people); </SPAN><LI class=""><SPAN>echo "</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></FONT></STRONG></SPAN><SPAN>---排序后---</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></FONT></STRONG></SPAN><SPAN>"; </SPAN></SPAN><LI class=alt><SPAN>foreach ($people as $mychrs) </SPAN><LI class=""><SPAN>echo $mychrs." "; </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN> </SPAN></SPAN></LI></OL>升序排序后的数组元素显示为 birth name nation sex,当然,sort()函数是区分字母大小写的(字母从大到小的顺序是:A…Z…a…z)
立即学习“PHP免费学习笔记(深入)”;
Sort()函数还具有第二参数,用来说明PHP数组排序升序的规则是用来比较数字还是字符串的。比如:
<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 class=""><SPAN>echo "---按数字升序排序---</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></FONT></STRONG></SPAN><SPAN>"; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>num2</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('26','3',); </SPAN></SPAN><LI class=""><SPAN>sort($num2,SORT_NUMERIC); </SPAN><LI class=alt><SPAN>foreach ($num2 as $mychrs) </SPAN><LI class=""><SPAN>echo $mychrs." "; </SPAN><LI class=alt><SPAN>echo "</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></FONT></STRONG></SPAN><SPAN>---按字符升序排序---</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></FONT></STRONG></SPAN><SPAN>"; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>num3</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('26','3'); </SPAN></SPAN><LI class=alt><SPAN>sort($num3,SORT_STRING); </SPAN><LI class=""><SPAN>foreach ($num3 as $mychrs) </SPAN><LI class=alt><SPAN>echo $mychrs." "; </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN> </SPAN></SPAN></LI></OL>SORT_NUMERIC和SORT_STRING用来声明按数字或字符的升序排列。如果按照数字升序排列是:3,26;但如果按照字符升序排列则是:26,3了。PHP中除了升序函数以外,还有降序或称反向排列的函数,就是rsort()函数,比如:$num1=range(1,9);rsort($num1);这里其实就相当于range(9,1)。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号