php学习笔记7-(数组操作)

来源:互联网 发布:java编译过程详解 编辑:程序博客网 时间:2024/05/29 16:29
php支持两种数组,一种是普通的数字索引的数组,从0开始,类似Java(为什么不说C呢?因为数组的数组是多维数组,然而子数组的长度可以是任意的,不必每个都相同,php中的数组都是这样的)。还支持另外一种数组,key/value进行映射的数组,相当于java中的Hashtable。数组的操作方式也是多种多样的,这里还提到了foreach的用法。
foreach ( $array as $value ) ...;
foreach ( $array as $key=>$value ) ...;

这次的程序同样是一个解释执行的普通脚本,而非一个服务器脚本。
#!/usr/bin/php
<?php

# array declare
$strs=array('a','b','c');
echo "Stage 1: ";
for ( $i=0$i<count($strs); $i++ )
    
echo "$strs[$i] ";
echo " ";

# array_push() / array_pop()     <--   operate like a stack
$numbers=array();
echo "Stage 2:  ";
for ( $i=0$i<10$i++ ) array_push($numbers,$i*3);  // append an element
foreach ( $numbers as $x ) echo "$x ";
echo " ";
while ( count($numbers)>0 ) 
    
echo array_pop($numbers)." ";
echo " ";

# key / value
echo "Stage 3:  ";
$prices=array('Lyre'=>250,'Shaka'=>600,'Sakura'=>20,'Aiakos'=>280);
foreach ( $prices as $key=>$value ) echo "$key : $value ";
echo " ";

# browse an array
echo "Stage 4:  ";
reset($prices);  # move the pointer to the start of the array
while ( list($product, $price)=each($prices) ) {  # here, each() returns the current element as an array
                                                  # and assigned to $product and $price by the result of
                                              # list(), then each() moves the pointer to the next element
    echo "$product : $price ";
}
# current(), each(), next(), prev(), pos(), reset(), end()
echo "----------------- ";
$value=end($prices);   // move to the end of the array
do {
    
echo $value." ";  # pos() 的返回让我很惊讶!
    $value=prev($prices);
while ( $value );
echo " ";

# 多维数组
echo "Stage 5:  ";
$multi_way=array(    # declare is so simple
    array(1,2,3,4,5),               # 5 elements
    array('this','is','an','array'# 4 elements
);
for ( $i=0$i<count($multi_way); $i++ ) :
    
for ( $j=0$j<count($multi_way[$i]); $j++ ) echo $multi_way[$i][$j].' '# do not include
                                                                                  # $multi_way[$i][$j] into
                                                                                  # a string
    echo " ";
endfor;
echo "------------------ ";
foreach ( $multi_way as $sub_arr ) :
    
foreach ( $sub_arr as $sub_value ) echo "$sub_value ";
    
echo " ";
endforeach;
echo " ";

# shuffle() - random an array
#
 range(start,end[,step]) - generate an array
#
 array_reverse() - reverse an array
#
 sort(), usort()(sort according to an user defined function), 
#
 asort()(sort an key/value array), ksort()(only sort the keys)
#
 rsort(), arsort(), krsort() <== sort by descendant order
echo "Stage 6:  ";
$tens=range(1,10,2);
foreach ($tens as $valueecho "$value "echo " ";
shuffle($tens);
foreach ($tens as $valueecho "$value "echo " ";
$tens=array_reverse($tens);
foreach ($tens as $valueecho "$value "echo " ";
sort($tens);
foreach ($tens as $valueecho "$value "echo " ";
rsort($tens);
foreach ($tens as $valueecho "$value "echo " ";
echo " ";

# count() and sizeof() are the same
#
 array_count_values 统计各元素个数。
echo "Stage 7:  ";
$arr=array(1,3,2,42,23,324,234,23,4,34,1,3,3,3);
$tj=array_count_values($arr);
foreach ( $tj as $key=>$value ) echo "$key$value ";
echo " ";

# extract() extract the key/values into variables
echo "Stage 8:  ";
$arr=array
    
'key1'=>'value1',
    
'key2'=>'value2',
    
'key3'=>'value3'
);
extract($arr);
echo "$key1 $key2 $key3 ";

# array_walk() applies a function to an array
echo "Stage 9:  ";
$arr1=range(1,10);
function print1($value) {
    
echo "$value ";
}
array_walk($arr1,'print1');
echo "------ ";
$arr2=array('num1'=>1,'num2'=>2,'num3'=>3);
function print2(&$value,$key,$user_data) {  # pay attention to &
    $value*=$user_data;
    
if ( $key=='num2' ) $value+=20;
}
array_walk(&$arr2,'print2',3);              # pay attention to &
foreach ( $arr2 as $key=>$value ) echo "$key$value ";
?>