PHP自学之路-----走进数组及相关数组函数
PHP数组:
数组时一组关键字和值得集合,值可以是任何一种类型;看下面简单的入门案例:
[php]
$hens[0]=3;
$hens[1]=5;
$hens[2]=2.5;
$hens[3]=4;
$hens[4]=3.5;
$hens[5]=17;
//为了计算数组元素的个数,使用系统函数count;
echo "最共有".count($hens)." 只鸡
";
$allwight=0;
for($i=0;$i
";
$allwight+=$hens[$i];
}
echo "最重量为:".$allwight.";平均重量为:".$allwight/count($hens);
?>
$hens[0]=3;
$hens[1]=5;
$hens[2]=2.5;
$hens[3]=4;
$hens[4]=3.5;
$hens[5]=17;
//为了计算数组元素的个数,使用系统函数count;
echo "最共有".count($hens)." 只鸡
";
$allwight=0;
for($i=0;$i
";
$allwight+=$hens[$i];
}
echo "最重量为:".$allwight.";平均重量为:".$allwight/count($hens);
?>
[plain] view plaincopyprint?
最共有6 只鸡
第0只鸡重量3
第1只鸡重量5
第2只鸡重量2.5
第3只鸡重量4
第4只鸡重量3.5
第5只鸡重量17
最重量为:35;平均重量为:5.8333333333333
最共有6 只鸡
第0只鸡重量3
第1只鸡重量5
第2只鸡重量2.5
第3只鸡重量4
第4只鸡重量3.5
第5只鸡重量17
最重量为:35;平均重量为:5.8333333333333创建数组
创建数组的方式很多,上面例子是最普通的一种方式。
在PHP数组中,每一个元素的值可以是任意的类型!
第二种方式:
$arr= array(1,90,"hello",null);
第三种方式创建数组
$arr["logo"] ="beijing";$arr["hsp"]=124;$arr[4]=678;
等同于下面的:
$arr=array("logo"=>"beijing","hsp"=>123,4=>678);
遍历方式:
foreach($arr as $key=>$val){
echo $key."=".$var."
";
}
第三种方式 指定数组不能用for循环访问,应该用上面的方式遍历。
[php]
//数组注意事项:
//我们在创建数组的时候,如果没有给某个元素制定下标,PHP会自动的用目前最大的那个下标值(整数),加上1作为该元素的下标。
$arr=array(5=>"logo",55,56);
$arr=array(5=>"logo",6=>55,7=>56);
//数组注意事项:
//我们在创建数组的时候,如果没有给某个元素制定下标,PHP会自动的用目前最大的那个下标值(整数),加上1作为该元素的下标。
$arr=array(5=>"logo",55,56);
$arr=array(5=>"logo",6=>55,7=>56);
数组的一些应用
[php]
// 创建一个简单的数组
$array = array(1, 2, 3, 4, 5);
print_r($array);
echo "
";
// 现在删除其中的所有单元,但保持数组本身的结构
foreach ($array as $i => $value) {
unset($array[$i]);
}
print_r($array);
echo "
";
// 添加一个单元(注意新的键名是 5,而不是你可能以为的 0)
$array[] = 6;
print_r($array);
echo "
";
// 重新索引:
$array = array_values($array);
$array[] = 7;
print_r($array);
echo "
";
var_dump($array);
?>
// 创建一个简单的数组
$array = array(1, 2, 3, 4, 5);
print_r($array);
echo "
";
// 现在删除其中的所有单元,但保持数组本身的结构
foreach ($array as $i => $value) {
unset($array[$i]);
}
print_r($array);
echo "
";
// 添加一个单元(注意新的键名是 5,而不是你可能以为的 0)
$array[] = 6;
print_r($array);
echo "
";
// 重新索引:
$array = array_values($array);
$array[] = 7;
print_r($array);
echo "
";
var_dump($array);
?> [plain]
执行结果:
执行结果:[plain]
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Array ( )
Array ( [5] => 6 )
Array ( [0] => 6 [1] => 7 )
array(2) { [0]=> int(6) [1]=> int(7) }
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Array ( )
Array ( [5] => 6 )
Array ( [0] => 6 [1] => 7 )
array(2) { [0]=> int(6) [1]=> int(7) }
PHP相关的常用函数
1、count
统计数组元素的个数,使用:count($arr);
2、is-array
判断是否为数组,is_array($arr);
3、print_r 和var_dump(可显示数组元素类型)
显示数组信息
4、explode(在实际开发中,用途比较大)
拆分字符串函数。explode(“按照什么拆分”,字符串)
5、foreach
数组遍历函数:
[plain]
foreach($arr as $key=>$val){
echo $key."=".$var."
";
}
foreach($arr as $key=>$val){
echo $key."=".$var."
";
}6、unset
unset()删除数组中的某个元素,但是不会重组索引。前面的例子已经说明!
7、array_values
重新索引,一般和上面的函数配合使用!具体看什么的例子
数组比较
[php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b: \n";
var_dump($c);
$c = $b + $a; // Union of $b and $a
echo "Union of \$b and \$a: \n";
var_dump($c);
?>
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b: \n";
var_dump($c);
$c = $b + $a; // Union of $b and $a
echo "Union of \$b and \$a: \n";
var_dump($c);
?> [plain]
Union of $a and $b:
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
}
Union of $b and $a:
array(3) {
["a"]=>
string(4) "pear"
["b"]=>
string(10) "strawberry"
["c"]=>
string(6) "cherry"
}
Union of $a and $b:
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
}
Union of $b and $a:
array(3) {
["a"]=>
string(4) "pear"
["b"]=>
string(10) "strawberry"
["c"]=>
string(6) "cherry"
}
[php]
比较数组
$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>
比较数组
$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>
[php]
//error_reporting(E_ALL^E_NOTICE);
$aar=$_REQUEST["grade"];
$arr=explode(",",$aar);
$allval=0;
foreach($arr as $val){
$allval+=$val;
}
?>
请输入学员的成绩,用逗号隔开
$aar=$_REQUEST["grade"];
$arr=explode(",",$aar);
$allval=0;
foreach($arr as $val){
$allval+=$val;
}
?>
echo "平均值:".round($allval/count($arr),2);
?>
//error_reporting(E_ALL^E_NOTICE);
$aar=$_REQUEST["grade"];
$arr=explode(",",$aar);
$allval=0;
foreach($arr as $val){
$allval+=$val;
}
?>
请输入学员的成绩,用逗号隔开
$aar=$_REQUEST["grade"];
$arr=explode(",",$aar);
$allval=0;
foreach($arr as $val){
$allval+=$val;
}
?>
echo "平均值:".round($allval/count($arr),2);
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function