📜  PHP |数组

📅  最后修改于: 2022-05-13 01:56:26.011000             🧑  作者: Mango

PHP |数组

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

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

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

索引或数字数组

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

PHP


PHP


PHP
"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";
 
?>


PHP
"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";
}
 
?>


PHP
 "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"];
 
?>


PHP
 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";
}
 
?>


输出:

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

遍历:我们可以在PHP中使用循环遍历索引数组。我们可以通过两种方式遍历索引数组。首先使用for循环,其次使用foreach。你可以参考PHP |用于语法和基本用途的循环。

例子:

PHP


输出:

Looping using foreach: 
Zack
Anthony
Ram
Salim
Raghav

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

关联数组

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

例子:

PHP

"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

"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

 "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

 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

PHP是一种专门为 Web 开发而设计的服务器端脚本语言。您可以按照此PHP教程和PHP示例从头开始学习PHP 。