PHP100 2019-03-27
1,快速创建数组的函数range()
比如range()函数可以快速创建从1到9的数字数组:
代码如下:
<?php $numbers=range(1,9); echo $numbers[1]; ?>
代码如下:
<?php $numbers=range(a,z); foreach ($numbers as $mychrs) echo $mychrs." "; ?>
2,PHP中常规数组的排序
一般数组中的各元素均以字符或数字表现的,所以可对数组元素进行升序排列,该功能函数为sort()。比如:
代码如下:
<?php $people=array('name','sex','nation','birth'); foreach ($people as $mychrs) echo $mychrs." "; sort($people); echo "<br />---排序后---<br />"; foreach ($people as $mychrs) echo $mychrs." "; ?>
代码如下:
<?php echo "---按数字升序排序---<br />"; $num2=array('26','3',); sort($num2,SORT_NUMERIC); foreach ($num2 as $mychrs) echo $mychrs." "; echo "<br />---按字符升序排序---<br />"; $num3=array('26','3'); sort($num3,SORT_STRING); foreach ($num3 as $mychrs) echo $mychrs." "; ?>
代码如下:
$num1=range(1,9); rsort($num1);
3,PHP中关联数组的排序
PHP除了支持数字索引数组以外,还支持相关数组。比如如下数组就是一个相关(关联)数组:
代码如下:
$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth');
代码如下:
<?php $peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth'); foreach ($peoples as $mychrs) echo $mychrs." "; echo "<br />--按元素值升序排列--<br />"; asort($peoples); foreach ($peoples as $mychrs) echo $mychrs." "; echo "<br />--按关键字升序排列--<br />"; ksort($peoples); foreach ($peoples as $mychrs) echo $mychrs." "; ?>
4,PHP数组元素随机排序
PHP中使用shuffle()函数将数组元素进行随机的重新排序,比如:
代码如下:
<?php $fer=array('cnbruce','cnrose','cnjames','cnanne'); shuffle($fer); foreach ($fer as $mychrs) echo $mychrs." "; ?>
5,PHP数组按原顺序反向排序
PHP中可使用array_reverse()函数将数组元素按原顺序反向排序。比如:
代码如下:
<?php $fer=array('cnbruce','cnrose','cnjames','cnanne'); foreach ($fer as $mychrs) echo $mychrs." "; $fer=array_reverse($fer); echo "<br />--按原顺序反向--<br />"; foreach ($fer as $mychrs) echo $mychrs." "; ?>
<?php. if (!empty($_POST)) {. $data1 = $_POST["data1"];$data2 = $_POST["data2"];$fuhao = $_POST["fuh