分类:
2008-04-18 08:45:38
在数组摘录部分了解到最基本的PHP数组的建立和数组元素的显示。需要深入学习下PHP数组的相关操作。首先接触的就是数组元素的升序、降序的排序问题。
1,快速创建数组的函数range()
比如range()函数可以快速创建从1到9的数字数组:
numbers=range(1,9); echo numbers[1]; ?> |
numbers=range(a,z); foreach (numbers as mychrs) echo mychrs." "; ?> |
people=array('name','sex','nation','birth'); foreach (people as mychrs) echo mychrs." "; sort(people); echo " ---排序后--- "; foreach (people as mychrs) echo mychrs." "; ?> |
echo "---按数字升序排序--- "; num2=array('26','3',); sort(num2,SORT_NUMERIC); foreach (num2 as mychrs) echo mychrs." "; echo " ---按字符升序排序--- "; num3=array('26','3'); sort(num3,SORT_STRING); foreach (num3 as mychrs) echo mychrs." "; ?> |
num1=range(1,9); rsort(num1); |
peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth'); |
peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth'); foreach (peoples as mychrs) echo mychrs." "; echo " --按元素值升序排列-- "; asort(peoples); foreach (peoples as mychrs) echo mychrs." "; echo " --按关键字升序排列-- "; ksort(peoples); foreach (peoples as mychrs) echo mychrs." "; ?> |
fer=array('cnbruce','cnrose','cnjames','cnanne'); shuffle(fer); foreach (fer as mychrs) echo mychrs." "; ?> |
fer=array('cnbruce','cnrose','cnjames','cnanne'); foreach (fer as mychrs) echo mychrs." "; fer=array_reverse(fer); echo " --按原顺序反向-- "; foreach (fer as mychrs) echo mychrs." "; ?> |