PHP数组用法教程和详细指南

2021年3月12日13:47:48 发表评论 845 次浏览

PHP中的数组是一种数据结构, 它使我们可以在单个变量下存储相似数据类型的多个元素, 从而节省了为每个数据创建不同变量的工作。数组有助于创建相似类型的元素列表, 可以使用它们的索引或键来访问它们。假设我们要存储五个名称并相应地打印它们。可以通过使用五个不同的字符串变量轻松完成此操作。但是, 如果数量从5个增加到100个, 那么对于用户或开发人员来说, 创建这么多不同的变量确实非常困难。数组在这里发挥了作用, 它帮助我们将每个元素存储在单个变量中, 并且还允许使用索引或键轻松访问。数组是使用array()PHP中的功能。

PHP基本上有三种类型的数组:

  • 索引或数值数组:具有数字索引的数组, 其中值线性存储。
  • 关联数组:一个具有字符串索引的数组, 其中可以为每个值分配一个特定的键, 而不是线性存储。
  • 多维数组:一个数组, 其中包含单个或多个数组, 可以通过多个索引进行访问。

索引或数值数组

这些类型的数组可用于存储任何类型的元素, 但是索引始终是数字。默认情况下, 索引从零开始。可以用两种不同的方式创建这些数组, 如以下示例所示:

<?php
  
// One way to create an indexed array
$name_one = array ( "Zack" , "Anthony" , "Ram" , "Salim" , "Raghav" );
  
// Accessing the elements directly
echo "Accessing the 1st array elements directly:\n" ;
echo $name_one [2], "\n" ;
echo $name_one [0], "\n" ;
echo $name_one [4], "\n" ;
  
// Second way to create an indexed array
$name_two [0] = "ZACK" ;
$name_two [1] = "ANTHONY" ;
$name_two [2] = "RAM" ;
$name_two [3] = "SALIM" ;
$name_two [4] = "RAGHAV" ;
  
// Accessing the elements directly
echo "Accessing the 2nd array elements directly:\n" ;
echo $name_two [2], "\n" ;
echo $name_two [0], "\n" ;
echo $name_two [4], "\n" ;
  
?>

输出如下:

Accessing the 1st array elements directly:
Ram
Zack
Raghav
Accessing the 2nd array elements directly:
RAM
ZACK
RAGHAV

遍历

:我们可以使用PHP中的循环遍历索引数组。我们可以通过两种方式遍历索引数组。首先使用for循环, 其次使用foreach。你可以参考

PHP |循环

用于语法和基本用法。

例子:

<?php
  
// Creating an indexed array
$name_one = array ( "Zack" , "Anthony" , "Ram" , "Salim" , "Raghav" );
  
// One way of Looping through an array usign foreach
echo "Looping using foreach: \n" ;
foreach ( $name_one as $val ){
     echo $val . "\n" ;
}
  
// count() function is used to count 
// the number of elements in an array
$round = count ( $name_one ); 
echo "\nThe number of elements are $round \n" ;
  
// Another way to loop through the array using for
echo "Looping using for: \n" ;
for ( $n = 0; $n < $round ; $n ++){
     echo $name_one [ $n ], "\n" ;
}
  
?>

输出如下:

Looping using foreach: 
Zack
Anthony
Ram
Salim
Raghav

The number of elements is 5 
Looping using for: 
ZACK
ANTHONY
RAM
SALIM
RAGHAV

关联数组

这些类型的数组与索引数组相似, 但不是线性存储, 而是可以为每个值分配一个用户定义的字符串类型的键。

例子:

<?php
  
// One way to create an associative array
$name_one = array ( "Zack" => "Zara" , "Anthony" => "Any" , "Ram" => "Rani" , "Salim" => "Sara" , "Raghav" => "Ravina" );
  
// Second way to create an associative array
$name_two [ "zack" ] = "zara" ;
$name_two [ "anthony" ] = "any" ;
$name_two [ "ram" ] = "rani" ;
$name_two [ "salim" ] = "sara" ;
$name_two [ "raghav" ] = "ravina" ;
  
// Accessing the elements directly
echo "Accessing the elements directly:\n" ;
echo $name_two [ "zack" ], "\n" ;
echo $name_two [ "salim" ], "\n" ;
echo $name_two [ "anthony" ], "\n" ;
echo $name_one [ "Ram" ], "\n" ;
echo $name_one [ "Raghav" ], "\n" ;
  
?>

输出如下:

Accessing the elements directly:
zara
sara
any
Rani
Ravina

遍历关联数组

:我们可以使用循环以与数字数组类似的方式遍历关联数组。我们可以通过两种方式遍历关联数组。首先使用for循环, 其次使用foreach。你可以参考

PHP |循环

用于语法和基本用法。

例子:

<?php
  
// Creating an associative array
$name_one = array ( "Zack" => "Zara" , "Anthony" => "Any" , "Ram" => "Rani" , "Salim" => "Sara" , "Raghav" => "Ravina" );
  
// Looping through an array using foreach
echo "Looping using foreach: \n" ;
foreach ( $name_one as $val => $val_value ){
     echo "Husband is " . $val . " and Wife is " . $val_value . "\n" ;
}
  
// Looping through an array using for
echo "\nLooping using for: \n" ;
$keys = array_keys ( $name_two );
$round = count ( $name_two ); 
  
for ( $i =0; $i < $round ; ++ $i ) {
     echo $keys [ $i ] . ' ' . $name_two [ $keys [ $i ]] . "\n" ;
}
  
?>

输出如下:

Looping using foreach: 
Husband is Zack and Wife is Zara
Husband is Anthony and Wife is Any
Husband is Ram and Wife is Rani
Husband is Salim and Wife is Sara
Husband is Raghav and Wife is Ravina

Looping using for: 
zack zara
anthony any
ram rani
salim sara
raghav ravina

多维数组

多维数组就是这样的数组, 它们在每个索引处存储另一个数组, 而不是单个元素。换句话说, 我们可以将多维数组定义为数组数组。顾名思义, 该数组中的每个元素都可以是一个数组, 并且它们还可以容纳其中的其他子数组。多维数组中的数组或子数组可以使用多维来访问。

例子:

<?php
  
// Defining a multidimensional array
$favorites = array (
     array (
         "name" => "Dave Punk" , "mob" => "5689741523" , "email" => "davepunk@gmail.com" , ), array (
         "name" => "Monty Smith" , "mob" => "2584369721" , "email" => "montysmith@gmail.com" , ), array (
         "name" => "John Flinch" , "mob" => "9875147536" , "email" => "johnflinch@gmail.com" , )
);
  
// Accessing elements
echo "Dave Punk email-id is: " . $favorites [0][ "email" ], "\n" ;
echo "John Flinch mobile number is: " . $favorites [2][ "mob" ];
  
?>

输出如下:

Dave Punk email-id is: davepunk@gmail.com
John Flinch mobile number is: 9875147536

遍历多维数组:我们可以使用for和foreach循环以嵌套方式遍历多维数组。也就是说, 一个for循环用于外部数组, 一个for循环用于内部数组。

例子:

<?php
// Defining a multidimensional array
$favorites = array (
     "Dave Punk" => array (
         "mob" => "5689741523" , "email" => "davepunk@gmail.com" , ), "Dave Punk" => array (
         "mob" => "2584369721" , "email" => "montysmith@gmail.com" , ), "John Flinch" => array (
         "mob" => "9875147536" , "email" => "johnflinch@gmail.com" , )
);
  
// Using for and foreach in nested form
$keys = array_keys ( $favorites );
for ( $i = 0; $i < count ( $favorites ); $i ++) {
     echo $keys [ $i ] . "\n" ;
     foreach ( $favorites [ $keys [ $i ]] as $key => $value ) {
         echo $key . " : " . $value . "\n" ;
     }
     echo "\n" ;
}
  
?>

输出如下:

Dave Punk
mob : 2584369721
email : montysmith@gmail.com

John Flinch
mob : 9875147536
email : johnflinch@gmail.com

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: