[PHP函数]PHP数组函数(数组的排序和遍历)

作者:笨小孩 发布于:2012-10-18 15:44 分类:PHP

<pre>
<?php
/*PHP数组函数(数组的排序和遍历)
*2009.12.08
*/
//1.sort()、rsort()
//对数组进行排序。为 array 中的单元赋予新的键名。这将删除原有的键名而不仅是重新排序。如果成功则返回 TRUE,失败则返回 FALSE。
//可选的第二个参数:SORT_REGULAR – 正常比较单元(不改变类型)| SORT_NUMERIC – 单元被作为数字来比较 | SORT_STRING – 单元被作为字符串来比较
echo ‘<b>1.sort()、rsort()</b><br />’;
$fruits = array(“lemon”, “orange”, “f”=>”banana”, “apple”);
sort($fruits);//升序
print_r($fruits);
echo ‘<br />————————————-<br />’;

//2.asort()、arsort()
//数组的索引保持和单元的关联。主要用于对那些单元顺序很重要的结合数组进行排序。
echo ‘<b>2.asort()、arsort()</b><br />’;
$fruits = array(“d” => “lemon”, “a” => “orange”, “b” => “banana”, “c” => “apple”);
arsort($fruits);//降序
print_r($fruits);
echo ‘<br />————————————-<br />’;

//3.ksort()、krsort()
//对数组按照键名排序。
echo ‘<b>3.ksort()、krsort()</b><br />’;
$fruits = array(“d”=>”lemon”, “a”=>”orange”, “b”=>”banana”, “c”=>”apple”);
ksort($fruits);//升序
print_r($fruits);
echo ‘<br />————————————-<br />’;

//4.natsort()、natcasesort()
//用“自然排序”算法对数组排序 。
echo ‘<b>4.natsort()</b><br />’;
$array1 = $array2 = array(“img12.png”, “img10.png”, “img2.png”, “img1.png”);
sort($array1);
echo “Standard sorting\n”;
print_r($array1);
natsort($array2);
echo “\nNatural order sorting\n”;
print_r($array2);
//natcasesort()不区分大小写字母。
echo ‘<b>natcasesort()</b><br />’;
$array1 = $array2 = array(‘IMG0.png’, ‘img12.png’, ‘img10.png’, ‘img2.png’, ‘img1.png’, ‘IMG3.png’);
sort($array1);
echo “Standard sorting\n”;
print_r($array1);
natcasesort($array2);
echo “\nNatural order sorting (case-insensitive)\n”;
print_r($array2);
echo ‘<br />————————————-<br />’;

//5.in_array()
//检查数组中是否存在某个值。
//如果第三个参数 的值为 TRUE 则 in_array() 函数还会检查类型
echo ‘<b>5.ksort()、krsort()</b><br />’;
$os = array(“Mac”, “NT”, “Irix”, “Linux”,123);
if (in_array(“Irix”, $os)) echo “Got Irix”;
if (in_array(“mac”, $os)) echo “Got mac”;
if (in_array(“123″, $os,TRUE)) echo “123″;
echo ‘<br />————————————-<br />’;

//6.current()、reset()、prev()、next()、end()//当前、第一个、前一个、下一个、最后一个
//数组指针的移动
echo ‘<b>6.current()、reset()、prev()、next()、end()</b><br />’;
$transport = array(‘foot’, ‘bike’, ‘car’, ‘plane’);
$mode = current($transport); echo $mode.’<br />’ ;// $mode = ‘foot’;
$mode = next($transport);    echo $mode.’<br />’ ;// $mode = ‘bike’;
$mode = current($transport); echo $mode.’<br />’ ;// $mode = ‘bike’;
$mode = prev($transport);    echo $mode.’<br />’ ;// $mode = ‘foot’;
$mode = end($transport);     echo $mode.’<br />’ ;// $mode = ‘plane’;
$mode = current($transport); echo $mode.’<br />’ ;// $mode = ‘plane’;
$mode = reset($transport);      echo $mode ;          // $mode = ‘foot’;
echo ‘<br />————————————-<br />’;

//7.list()、each()
//仅能用于数字索引的数组并假定数字索引从 0 开始。
echo ‘<b>7.list()、each()</b><br />’;
$fruit = array(‘a’ => ‘apple’, ‘b’ => ‘banana’, ‘c’ => ‘cranberry’);
reset($fruit);
while (list($key, $val) = each($fruit)) {
echo “$key => $val\n”;
}
echo ‘<br />————————————-<br />’;

//8.key()
// 返回数组中当前单元的键名。
echo ‘<b>8.key()</b><br />’;
$array = array(‘fruit1′ => ‘apple’,'fruit2′ => ‘orange’,'fruit3′ => ‘grape’,'fruit4′ => ‘apple’,'fruit5′ => ‘apple’);
// this cycle echoes all associative array
// key where value equals “apple”
while ($fruit_name = current($array)) {
if ($fruit_name == ‘apple’) {
echo key($array).’<br />’;
}
next($array);
}
echo ‘<br />————————————-<br />’;
?>

标签: PHP函数

发表评论:

手机版 | 
Copyright © 2012 lamp46.com All Right Reserved. sitemap