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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
