统计数组中元素个数

统计数组总元素个数count或sizeof

[cce_php]

<?php
$array = array( “a”,”b”,”c”,1,2,3);
echo count($array);

echo “<br />”;

echo sizeof($array);
?>

[/cce_php]

统计某一个数组元素在数组中出现的次数:

array_count_values

[cce_php]

<?php
$array = array( “a”,”b”,”c”,1,2,3,3,”a”,”a”);
$array_num = array_count_values($array);
while (list($key,$value) = each($array_num)){

echo “$key => $value <br />”;
}

[/cce_php]

 

对数组中任何元素操作函数

array_walk对数组中每一个值操作:

[cce_php]

<?php
$array = array( “a”,”b”,”c”,1,2,3);

function print_array($value){
echo “$value<br />”;
}
array_walk($array,’print_array’);
$myarray = array(“name”=>”zhangsan”,”number”=>”12″,”class”=>”one”);

function print_myarray($value,$key){
echo “$key=>$value<br />”;
}

//关联数组先为值,后为关键字
array_walk($myarray,’print_myarray’);
?>

[/cce_php]

 

PHP数组指针操作

each() 数组当前指针元素;

current() 数组第一个指针元素;

next() 数组下一个指针元素;

prev() 数组上一个指针元素;

reset() 返回指针到第一个元素;

end() 将指针移动数组结尾

 

PHP提取文本文件到数组

[cce_php]

<?php
//Create short variable name
$DOCUMENT_ROOT = $_SERVER[‘DOCUMENT_ROOT’];
?>
<html>
<head>
<title>
Bob’s Auto Parts – Customer Orders
</title>
</head>

<body>
<h1>Bob’s auto Parts</h1>
<h2>Customer Orders</h2>
<?php
//Read in the entire file.
//Each order becomes an element in the array

$orders= file(“$DOCUMENT_ROOT/../2015/orders.txt”);
//读取所有文件到$orders函数
$number_of_orders = count($orders);
//定义总行数
if ($number_of_orders == 0) {
echo “<p><strong>No orders pending. Please try again later.</strong></p>”;
}
echo “<table border=\”1\”>”;
echo “<tr><th bgcolor=\”#CCCCFF\”>Order Date</th>”;
echo “<th bgcolor=\”#CCCCFF\”>Tires</th>”;
echo “<th bgcolor=\”#CCCCFF\”>Oil</th>”;
echo “<th bgcolor=\”#CCCCFF\”>Spark</th>”;
echo “<th bgcolor=\”#CCCCFF\”>Total</th>”;
echo “<th bgcolor=\”#CCCCFF\”>Address</th></tr>”;

for ($i=0; $i<$number_of_orders; $i++) {
$line = explode(“\t”, $orders[$i]);

//将字符分割成以’\t’为分隔的小块

$line[1]= intval($line[1]);
$line[2]= intval($line[2]);
$line[3]= intval($line[3]);

//将字符转换为整数

echo “<tr>

<td>”.$line[0].”</td>
<td align=\”right\”>”.$line[1].”</td>
<td align=\”right\”>”.$line[2].”</td>
<td align=\”right\”>”.$line[3].”</td>
<td align=\”right\”>”.$line[4].”</td>
<td>”.$line[5].”</td>
</tr>”;
}

echo “</table>”;
?>

</body>
</html>

[/cce_php]

php数组排序

数组排序:

1.sort 非关联数组字母顺序或数字大小排序:

[cce_php]

$products = array( ‘Tires’, ‘Oil’, ‘Spark Plugs’);
echo $products[0].”<br />”;
sort($products);
echo $products[0].”<br />”;

$num = array(10,50,1,20,30,60,55);
echo $num[0].”<br />”;
sort($num);
echo $num[0];

[/cce_php]

2.关联数组排序:

asort关联数组元素值排序

[cce_php]

$prices = array( ‘Tires’=>100,’Oil’ =>10 ,’Spark Plugs’ => 4);
foreach($prices as $key=>$value){
echo $key.”=>”.$value.”<br />”;
}
asort($prices);
echo “<br />”;
foreach($prices as $key=>$value){
echo $key.”=>”.$value.”<br />”;
}

[/cce_php]

ksort 关联数组按元素的键值排序

[cce_php]

$prices = array( ‘Tires’=>100,’Oil’ =>10 ,’Spark Plugs’ => 4);
foreach($prices as $key=>$value){
echo $key.”=>”.$value.”<br />”;
}
ksort($prices);
echo “<br />”;
foreach($prices as $key=>$value){
echo $key.”=>”.$value.”<br />”;
}

[/cce_php]

自定义数组排序,使用自定义函数返回值的正负来确定是升序还是降序

[cce_php]
<?php
$products = array( array( 'TIR', 'Tires', 100),
				   array( 'OIl', 'Oil', 10),
				   array( 'SPK', 'Spark Plugs', 4));
function compare($x, $y){
	if ($x[2] == $y[2]) {
		return 0;
		} else if ($x[2] < $y[2]) {
			return -1;
			} else {
				return 1;
				}
	}

usort ($products, 'compare');

while (list($product, $price) = each( $products)) {
	while (list($product1 , $price1) = each($products[$product])){
		echo "$product1 - $price1"."<br />";
		}
	
	}
?>
[/cce_php]

php数组操作

定义数组:

1.直接赋值

普通数组
$products = array(“Tires”, “Oil”, “Spark Plugs”);

关联数组
$prices = array(‘Tires’=>100, ‘Oil’=>10, ‘Spark Plugs’=>4);

2.range函数赋值

$numbers = range(1,10)  10个数据元素值为1-10

$numbers = range(1,10,2) 1-10中为奇数的数据元素值

 $numbers = range(‘a’,’z’) a到z数据元素值

3.数组首元素赋值

$numbers[0] = 1;
$number[‘tires’] = 10;

访问数组:

1.循环访问使用:
案例1 for(有序)

$numbers = range(‘a’,’z’);
for ($i = 0; $i < 26 ;$i++) {
echo $numbers[$i].”<br />”;
}

案例2 foreach(无序)
$products = array(“Tires”, “Oil”, “Spark Plugs”);
foreach ($products as $current){
echo $current;
}
$prices = array(‘Tires’=>100, ‘Oil’=>10, ‘Spark Plugs’=>4);
foreach ($prices as $key => $value) {
echo $key.”-“.$value.”<br />”;
}
案例三 while + each()
$prices = array(‘Tires’=>100, ‘Oil’=>10, ‘Spark Plugs’=>4);
while ($element = each($prices)){
echo $element[‘key’];
echo “-“;
echo $element[‘value’];
echo “<br>”;
}
案例四 while + list() +each()
$prices = array(‘Tires’=>100, ‘Oil’=>10, ‘Spark Plugs’=>4);
while (list($product, $price) = each($prices)) {
echo “$product – $price<br />”;
}

数组指针:

1.reset() 将数组指针定位到开头,重新遍历数组时需用此函数归位

多维数组遍历:

案例1

 $products = array ( array( 'TIR', 'Tires', 100), array('OIL', 'Oil', 10), array('SPK', 'Spark Plugs', 4)); echo $products[0][0]; for ($i=0; $i<3; $i++) { for ($j=0; $j<3; $j++){ echo $products[$i][$j]."\t"; } echo "<br />"; }

 案例二

 $products = array( array( 'Code'=> 'TIR', 'Description'=> 'Tires', 'Price'=> 100), array( 'Code'=> 'OIL', 'Description'=> 'Oil', 'Price'=>10), array( 'Code'=> 'SPK', 'Description'=> 'Spark Plugs', 'Price'=>4) ); for ($i=0; $i<3; $i++) { foreach( $products[$i] as $key => $value){ echo $value."\t"; } echo "<br />"; }

案例三:

[cce_php]
$products = array( array( 'Code'=> 'TIR', 'Description'=> 'Tires', 'Price'=> 100),
				   array( 'Code'=> 'OIL', 'Description'=> 'Oil', 'Price'=>10),
				   array( 'Code'=> 'SPK', 'Description'=> 'Spark Plugs', 'Price'=>4)
				   );
for ($i=0; $i<3; $i++) {
	while(list($key, $value) = each($products[$i])){
		echo $value."\t";		
		}
	echo "<br />";
[/cce_php]

 案例四

[cce_php]
	
$categories = array( array( array ( 'CAR_TIR', 'Tires', 100),
							array ( 'CAR_OIL', 'Oil', 10),
							array ( 'CAR_SPK', 'Spark Plugs', 4),
							),
					 array( array ( 'VAN_TIR', 'Tires', 120),
					 		array ( 'VAN_Oil', 'Oil', 12),
							array ( 'VAN_spk', 'Spark Plugs', 5),
							),
					 array( array ( 'TRK_TIR', 'Tires', 150),
					 		array ( 'TRK_OIl', 'Oil', 15),
							array ( 'TRK_SPK', 'Spark Plugs', 6)
						    )
				   );
for($i=0;$i<3;$i++){
	for($j=0;$j<3;$j++){
		for($h=0;$h<3;$h++){
			echo $categories[$i][$j][$h]."\t";			
			}
		echo "<br />";
		}
	echo "<br />";
	}
[/cce_php]

PHP文件操作函数

file_exists 判断文件是否存在:

file_exists(“$DOCUMENT_ROOT/../2015/orders.txt”)

filesize 确定文件大小:

filesize(“$DOCUMENT_ROOT/../2015/orders.txt”)

nl2br 将 \n 换行符转换成 html <br />:

nl2br(fread($fp,filesize(“$DOCUMENT_ROOT/../2015/orders.txt”)))

unlink删除文件

unlink(“$DOCUMENT_ROOT/../2015/orders.txt”)

文件指针:

rewind 文件指针复位到开始,

rewind($fp)

ftell 文件指针当前位置

ftell($fp)

fseek 文件指针移动到某位:

fseek($fp,字节)

文件锁定:

flock锁定文件:

操作值:LOCK_SH 读锁定 ; LOCK_EX 写锁定;

LOCK_UN 释放锁定;LOCK_NB 防止加锁时阻塞

flock($fp,操作值)

PHP读文件常用函数

fopen打开文件:

rb:二进制只读打开

feof判断文件指针;

读取一行:

1.fgets每次读取一行文字;

2.fgetss每次读取一行文字并清除其中的HTML和PHP标记;

3.fgetcsv每次读取一行文字并使用定界符将其分隔:

fgetcsv($fp,999,”\t”)

@$fp= fopen( “$DOCUMENT_ROOT/../2015/orders.txt”,’rb’);
if (!$fp) {
echo “<p><strong>No orders pending. please try again later.</strong></p>”;
exit;
}
while (!feof($fp)) {
$order = fgets($fp, 999);
echo $order.”<br />”;
}

读取整个文件:

1.readfile读取整个文件:

readfile(“$DOCUMENT_ROOT/../2015/orders.txt”);

2.fpassthur使用文件指针读取,未测试成功。

3.file将文件内容读取到一个数组中,每一行为一个数组原素:

$filearray = file(“$DOCUMENT_ROOT/../2015/orders.txt”)

[cce_php]

<?php
//Create short variable name
$DOCUMENT_ROOT =$_SERVER[‘DOCUMENT_ROOT’];
$orders = file(“$DOCUMENT_ROOT/../2015/orders.txt”);
$number_of_orders = count($orders);
if ($number_of_orders == 0) {
echo “<p><strong>No orders pending. Please try again later.</strong></p>”;
}
for($i=0; $i<$number_of_orders;$i++){
echo $orders[$i].”<br />”;
}
?>

[/cce_php]

读取每一个字符:

fgetc读取每一个字符:

while(!feof($fp)) {
$char = fgetc($fp);
echo ($char == “\n” ? “<br />” : $char);

}

读取任意长度内容:

fread读取到文件尾或包结束,未测试成功

 

 

 

PHP文件写入三步曲

fopen()打开文件信息:

目录中加入..代表父目录增加文件安全

ab:二进制追加,wb:二进制重写

fwrite()写入文件信息;

fopen()关闭文件。

范例:

$fp = fopen(“$DOCUMENT_ROOT/../2015/orders.txt”,’ab’);

if (!$fp) {
echo “<p><strong>Your order could not be processed at this time. Please try again later\.</strong></p>”;
exit;
}
$outputstring = $date.”\t”.$tireqty.” tires \t”.$oilqty.” oil\t”.$sparkqty.” spark plugs\t\$”.$totalamount.”\t “.$address.”\n”;
fwrite($fp,$outputstring,strlen($outputstring));
fclose($fp);