首页  >  文章  >  后端开发  >  PHP 中的二维数组

PHP 中的二维数组

WBOY
WBOY原创
2024-08-29 12:43:14784浏览

数组是任何数据类型的元素的集合。 php中有很多数据类型,如字符串、整数、布尔值、数组、对象、资源……等。二维数组是这些数据类型的混合,主要是数组。 PHP 中有以下三种不同类型的二维数组:

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

  • 数值数组
  • 关联数组
  • 多维数组

PHP 中的二维数组类型

这三个数组解释如下:

1.数字数组

带有数字索引的数组。

语法:

array(value1, value2, value3, …);

示例

$input = array(10,20,30,40,50);

2.关联数组

具有字符串或数字索引的数组。该数组的元素以键值对的形式存储。

语法:

array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3',…);

示例

$input =  array(0 =>Emma, 1=>Alice, 2=>'John');

3.多维数组

数组的数组是多维数组或二维数组或嵌套数组。此格式始终是数组或数组。因此称为嵌套数组。

语法:

array (
array (elements...),
array (elements...),
...
)

示例:

$input = array(
array( "red", "green", "blue" ),
array( "yellow", "black", "white" )
);

在上面的示例中,输入数组是二维数组的示例。在这里,主数组包含 2 个元素,其中每个元素本身又是一个包含 3 个元素的数组。

如何定义二维数组?

我们了解到,在二维数组中,值元素是一个数组,并且还可能有子数组。数组中提到的维度采用行和列的形式。记住数组的表格格式,可以更轻松地学习如何定义这些数组。意思是,如果是二维数组,将使用两个索引,类似地,如果是三维数组,将使用三个索引,依此类推。

如何创建二维数组?

既然我们知道如何定义二维数组,我们现在就可以创建它了。这里索引没有定义,默认是一个始终以 0 开头的数字。

$input=array(
array( "red", "green", "blue" ),
array( "yellow", "black", "white" )
);

数组也可以以关联数组的形式定义。

(in key =>value form)

索引或键是一个字符串,如颜色、水果和汽车。值元素采用数组的形式,每个元素包含 3 个元素。

$input = array(
'colors'=>array ("Red", "Green", "Blue"),
'fruits'=>array ("Apple", "Orange", "Grapes"),
'cars'=>array ("BMW", "Skoda", "Mercedes")
);

如何访问二维数组的元素?

要访问这些数组值,您可以使用方括号。当您深入二维数组的更多级别时,方括号组的使用将随着每个级别的增加而增加。

示例#1

代码:

$input = array (
'colors' =>array ("Red", "Green", "Blue"),
'fruits' =>array ("Apple", "Orange", "Grapes"),
'cars' =>array ("Skoda", "BMW", "Mercedes")
);

请记住,第一组方括号包含键,在本例中是颜色、水果、汽车。接下来是一组方括号,用于向下遍历下一层,可以使用 0,1,2 等数字进行访问。

所以如果我们想访问上面数组中的元素“Grapes”,

echo $input['fruits'][2];

类似下面的例子

如果我们想访问数组中的元素“Mercedes”,那么

echo $input['cars'][2];

如果我们想访问数组中的元素“Red”,那么

echo $input['colors'][0];

因为数组中索引总是从 0 开始。

示例#2

代码:

$input = array (
array ("Red", "Green", "Blue"),
array ("Yellow", "Orange", "Purple"),
);

如果我们想访问上面数组中的元素“Orange”,我们将使用以下行

echo $input[0][1];

将给出“绿色”

echo $input[1][2];

将给出“紫色”

echo $input[0][0];

将给出“红色”

如何在 PHP 中插入二维数组的元素?

既然我们知道如何定义、创建和访问数组元素,我们现在将学习如何在数组中插入元素。 PHP 中定义了一些数组函数来处理多维数组,例如用于插入的 array_push() 函数、用于删除的 array_shift() 函数等等。

$input = array (
'colors'=>array ("Red", "Green", "Blue"),
'fruits'=>array ("Apple", "Orange", "Grapes"),
'cars'=>array ("Skoda", "BMW", "Mercedes")
);

使用 print_r() 函数,我们将首先按原样打印数组。

代码:

//create multidimensional array
$input = array (
"colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
// print the multidimensional array
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

输出:

PHP 中的二维数组

现在我们将使用

向水果子数组添加一个元素
array_push() function

语法:

array_push(array, value1,value2...)

哪里,

  • 数组是$input数组
  • value1 是要添加到数组中的元素
  • value2、value3 可选

示例#1

代码:

$input = array (
"colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
array_push($input['colors'], "Black");
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

输出:

PHP 中的二维数组

在下面的程序中,我们只是删除了键“colors”,并发现它使用 0 键附加到给定数组的最后一个,如输出图像所示。

Example #2

Code:

// create multidimensional array
$input = array (
"colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
// adding a value to array
array_push($input, "Black");
// print the multidimensional array
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

Output:

PHP 中的二维数组

Example #3

Code:

//create multidimensional array
$input = array (
array ("Red", "Green", "Blue"),
array ("Yellow", "Orange", "Purple")
);
//add a color to the array
array_push($input, "Black");
// print the multidimensional array
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

Output:

PHP 中的二维数组

How to Update Elements of 2D Arrays in PHP?

To update an element of the 2D array just get the key from the array and replace the value of that key in a particular array.

$input['cars']['Mercedes']  = 'Duster';

Example #1

Code:

//create multidimensional array
$input = array (
"colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
//update the Mercedes with Duster
$input["cars"][2]  = "Duster";
// print the multidimensional array
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

Output:

PHP 中的二维数组

Example #2

Code:

//create multidimensional array
$input = array (
array ("Red", "Green", "Blue"),
array ("Yellow", "Orange", "Purple")
);
//update the Mercedes with Duster
$input[0][1] = "White";
// print the multidimensional array
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

Output:

PHP 中的二维数组

How to Delete Elements of 2D Arrays?

To delete an element of the 2D array we will use array_shift() function.

array_shift removes and returns the first element value of the array.

Syntax:

array_shift(array)

where

-array is the $input array

Example #1

Code:

//create multidimensional array
$input = array (
"colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
//print the removed element
print_r(array_shift($input));

Output:

PHP 中的二维数组

Example #2

Code:

//create multidimensional array
$input = array (
array ("Red", "Green", "Blue"),
array ("Yellow", "Orange", "Purple")
);
//print the removed element
print_r(array_shift($input));

Output:

PHP 中的二维数组

Two dimensional in Associative Array

In the following example, we have created a 2-d array containing the information of books like the author of the book, type of book, and published in the year. Also, we will learn how to traverse or loop through this array. Looping through the multidimensional array we will use a nested foreach loop. Meaning one foreach loop inside another foreach loop. The same can also be done using for loop.

$input = array(
"The_Alchemist" => array (
"author" => "Paulo Coelho",
"type" => "Fiction",
"published_year" => 1988
),
"Managing_Oneself" => array (
"author" => "Peter Drucker",
"type" => "Non-fiction",
"published_year" => 1999
),
"Measuring_the_World" => array(
"author" => "Daniel Kehlmann",
"type" => "Fiction",
"published_year" => 2005
)
);

Just printing the above array without any loop will give us the following output:

Code:

// create multidimensional array
$input = array(
"The_Alchemist" => array (
"author" => "Paulo Coelho",
"type" => "Fiction",
"published_year" => 1988
),
"Managing_Oneself" => array (
"author" => "Peter Drucker",
"type" => "Non-fiction",
"published_year" => 1999
),
"Measuring_the_World" => array(
"author" => "Daniel Kehlmann",
"type" => "Fiction",
"published_year" => 2005
)
);
// print the plain multidimensional array
echo "
";
print_r($input);
echo "
";

Output:

PHP 中的二维数组

Now we will print the multidimensional array using a foreach loop.

Code:

// create multidimensional array
$input = array(
"The_Alchemist" => array (
"author" => "Paulo Coelho",
"type" => "Fiction",
"published_year" => 1988
),
"Managing_Oneself" => array (
"author" => "Peter Drucker",
"type" => "Non-fiction",
"published_year" => 1999
),
"Measuring_the_World" => array(
"author" => "Daniel Kehlmann",
"type" => "Fiction",
"published_year" => 2005
)
);
//foreach to loop the outer array
foreach($input as $book) {
echo "
"; // foreach to loop the inner array foreach($book as $key=>$value) { echo $key." ". $value. "
"; } }

Output:

PHP 中的二维数组

Conclusion

I hope this article is helpful to learn the concepts of the topic on a 2D array in PHP. This topic covers all the concepts required for the understanding related to the 2D array in PHP. This topic is made simpler with the help of examples with the output snapshots to refer to. According to the article, if all the programs are practiced well will surely help you to grasp the concepts easily. I hope the topic is made more informative for gaining more knowledge.

以上是PHP 中的二维数组的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn