Home  >  Article  >  Backend Development  >  PHP学习系列7

PHP学习系列7

WBOY
WBOYOriginal
2016-06-13 13:13:17810browse

PHP学习系列七
数据库操作
数组:
$product = array('tires','oil','spark plugs');
array是一个语言结构,而不是函数。
$product[0],$product[1]
foreach($produce as $current){
echo $current." ";
}

关联数组:
$prices=array('Tires'=>100,'oil'=>10,'spark plugs'=>4)
访问:$prices['Tires']
遍历:foreach或list()和each()结构
foreach($prices as $key => $value){
  echo $key." - ".$value."
";
}
while($element = each($prices)){
echo $element['key'];
echo "-";
echo $element['value'];
echo "
";
}
while(list($product,$price) = each($prices)){
  echo "$product-$price
";
}
list()将从each()返回的数组中所包含0,1两个元素变成为两个名为$product和$price的新变量。
如果希望在相同脚本中两次使用该数组,就必须使用函数reset()将当前元素重新设置到数组开始处。再次遍历,使用如下:
reset($prices);
while(list($product,$price)=each($prices)){
   echo "$produce - $price
";
}
sort():数组排序.区分大小写。大写字母都在小写字母前面。
asort()
ksort():对关联数组排序。
asort()根据每个元素值排序。ksort()按关键字排序。
反向排序:rsort(),arsort(),krsort().
使用usort()告诉php如何比较各个元素,需要编写自己的比较函数。相当与java中实现compareTo()函数。
function compare($x,$y){
if($x[1] == $y[1]){
return 0;
}else if($x[1] return -1;
}else{
return 1;
}
}
usort($product,'compare');
如果要让数组按另一种顺序存储,只要编写一个不同的比较函数。
uasort(),uksort()






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