什么是 PHP 数组?

王林
发布: 2024-08-29 12:42:37
原创
180 人浏览过

PHP 中的数组是一种数据结构,它使我们能够省去创建不同变量的工作,以便在单个变量下存储具有相似数据类型的多个元素。数组有助于创建相似元素的列表,可通过索引或键访问。此处使用数组将每个元素存储在一个变量中,并且还可以通过索引或键轻松访问。使用 PHP 中的函数,生成一个数组。

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

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

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

PHP 中的数组是什么?

数组不过是允许在一个名称下存储多个值的数据结构。简单来说,它是一个能够包含多个值的变量。分配给数组的值本质上是同质的,这意味着所有值的数据类型应该相同。如果数组应该只包含整数值,那么就不可能在其中存储字符或字符串值。

数组也可以被视为项目的集合,它遵循特定的顺序来存储项目或值。创建数组后,我们可以为其分配多个值。在 C、C++ 等语言中,开发人员最初应该指定数组的大小,但在 PHP 中,我们可以随意添加所需数量的项目,数组的大小也会相应增加。

理解 PHP 数组

现在我们将尝试使用语法和示例来理解 PHP 中的数组。 PHP 数组是通过使用数组函数实现的,该函数写为“array()”。它有一个非常简单的语法,使得数组的使用非常容易。以下是在 PHP 中声明和初始化数组的语法。

$array_name = array("value1", "value2", "value3",..)
登录后复制

这里,

  • Array_name 是将由值组成的数组的名称。
  • Array() 是 PHP 中帮助实现数组机制的函数。
  • 双引号内的值是分配给数组的值。

使用上面的语法,我们可以将值分配给数组。好吧,你能猜出我们如何使用存储在数组中的值吗?让我们来了解如何使用数组值的语法。

$array_name[index]
登录后复制

这里,

  • Array_name 是我们在上述语法中定义的数组的名称。
  • 索引是值的位置。例如,“value1”的索引值为 0。因此,要使用它们 value1,我们必须将其写成 $array_name[0].

PHP 数组如何让工作变得如此简单?

数组使得程序的使用这一事实是不容置疑的。在本节中,我们将了解数组实际上如何使工作变得轻松,并且我们将考虑一些示例来准确了解 PHP 数组的功能。

示例:下图将接受多个学生的姓名并将其打印出来。

<?php
$student_name=array("Amit", "Raj", "Dhiraj", "Shyam");
echo $student_name[0];
echo "<br>";
echo $student_name[1];
echo "<br>";
echo $student_name[2];
echo "<br>";
echo $student_name[3];
?>
登录后复制

输出:上面的程序将打印所有学生的姓名,下面是输出。

阿米特
拉吉
迪拉吉
夏姆

下图会让你清楚数组的概念。它显示了为添加到数组的项目分配值的顺序。

什么是 PHP 数组?

PHP 数组的类型

在 PHP 中,我们有两种类型的数组:一维数组和多维数组。

1. One-dimensional array

  • The one-dimensional array may be defined as the type of array that consists of multiple values assigned to the single array.
  • The Values assigned in the one-dimensional array are homogenous in nature that means it allows only the values with the same data type to be added to the array.
  • Syntax will be like.
    $array_name = array("value1", "value2", "value3")
    登录后复制
  • The way to use the values from the single dimensional array is the same that we have discussed above. $array_name[2] could be used to bring “Value3” in use.

2. Multi-dimensional array

  • A multi-dimensional array may be defined as the kind of array that allows us to assign values in multiple arrays at the same time.
  • The values assigned to the multi-dimensional array could be heterogeneous that means values of different data types could be stored in the arrays and it has to be taken care that the sequence of items of specific data types should be same in all the arrays.
  • Here we will look on two-dimensional array syntax. Well, just for your information, the complexity of array increases as the dimension of the array increases.
$array_name = array
(
array("String1", "Int1", "Int11"),
array("Stirng2", "Int2", "int22"),
array("String3", "Int3", "int33"),
array("String4", "Int4", "int44"),
)
登录后复制
  • To access the value stored in two-dimensional array we will use below syntax.
$array_name[0][0] will represent "String1"
$array_name[0][1] will represent "int1"
$array_name[0][2] will represent "int11"
$array_name[2][0] will represent "String3"
$array_name[2][1] will represent "int3"
$array_name[2][2] will represent "int33"
登录后复制

What can you do with PHP Array?

Array in PHP is very helpful while dealing with complex programs. It plays a sheer crucial role in order to make the program less bulky. For illustration, in order to store 10 values we will need 10 different variables but by the use of an array in PHP, we can store 10 different values of the same data type in one array. By the virtue of array, the program could be made very efficient as it helps to save the memory that is usually consumed by the allocation of variables

For simple programs, a one-dimensional array could be very helpful and if the program is somewhat complex than a developer can leverage the multi-dimensional array to get the expected outcome. In PHP, there is no need to assign the data type of the array. It automatically detects the data type by analyzing the values or items assigned to it. In addition to all the features of an array, it’s characteristic of increasing the size automatically makes it favorite among the developers.

Working with PHP array

In the above sections, we learned how an array is implemented in PHP, the way it stores the value and how we can use the values assigned to the array. Now in this section, we see how array could be used together with some of the inbuilt functions to get the desired output. As we know the best way to learn anything is facilitated by examples, the same way we will have a look in an example to dive deeper on working with an array.

Example: In this example, we will use count function with the array to find the total number of students in a school.

<?php
$studnet_name[0]="Amit";
$studnet_name[1]="Raj";
$studnet_name[2]="Dhiraj";
$studnet_name[3]="Shyam";
$arraylenght = count($studnet_name);
echo "The total number of students is" . $arraylenght;
?>
登录后复制

Output: The count function will count the number of items assigned to the array and that will help us find the total number of students.

The total number of students is 4

The way we added items to the array is different that we used initially. It has been used here this way to give you an idea of the alternate way of assigning values to the array. Name of the four students are assigned to each index of the array and could be used by choosing the index accordingly. let’s see another example where we will be using the sort and count function collectively to sort the values in the array.

Example: In this example, we will assign several roll numbers to the array in some random order and then will make the use of sort function to arrange the order of roll numbers.

<?php
$roll_number= array("22", "13", "2", "9");
sort($roll_number);
$roll_length = count($roll_number);
echo "Roll numbers in ascending order :"
for($x = 0; $x < $roll_length; $x++)
{
echo $roll_number[$x] . " ";
}?>
登录后复制

Output: The roll numbers that are assigned to the array in the random series will get sorted in ascending order.

Roll numbers in ascending order: 2 9 13 22

Advantages

We have been watching out the pros of using the PHP array and now we will be arranging those in a single section. Below are some of the advantages of PHP array that makes it very useful.

1. Multiple values under one name

An Array allows us to add multiple items of a similar type under one name which makes the values easy to access and convenient as well. Though the values are supposed to be of the same data type, we will not need to define the type of the array.

2. Endorse data structure implementation

The data structures such as linked list, trees, queues are implemented in any high-level programming language by the use of an array. Due to this advantage of an array, the languages could provide a platform to work on complex applications as well. It actually facilitates the way data has to be stored in the memory.

3.矩阵表示

多维数组允许开发人员通过以与数组中保存的方式相同的方式排列值来执行矩阵运算。使用矩阵时必须注意的唯一注意事项是,输出应该是一维数组或二维数组,我们相应地选择了输出数组。

4.容易记住

我们需要记住的是索引号,以便找到特定的值。无需记住多个变量名称,因为所有值都可以使用相同的名称来调用或使用,这实际上是数组名称。

5.内存利用率较低

变量数量的增加也会增加程序的内存利用率,因为需要将内存分配给每个变量,但如果是数组,则必须将内存分配给可以存储多个值和相同不必要的值的单个数组内存消耗。

所需技能

要使用 PHP 数组,您应该了解一些有关编程的基本知识,例如如何将值分配给数组,应该如何调用它们等等。为了使用数组实现数据结构,必须了解数据结构部分以及数组的工作概念。

我们还需要了解变量,因为数组只不过是变量的扩展版本。变量存储单个值的方式,与数组使用索引存储单个值的方式相同,通过多个索引的平均值相加的单个值组合在一起形成一个数组。任何对编程基础有深入了解的人都可以非常轻松地使用数组。

为什么我们需要 PHP 数组?

为了得到这个问题的答案,我们必须提醒我们到目前为止在本教程中所理解的所有内容。很明显,我们更喜欢使用任何东西,只有当它为我们提供某种优势时,并且由于数组在各个方面对我们非常有利,所以我们总是更喜欢在需要时使用数组。在编写代码时,每当我们需要为某个常见实体存储多个值时,我们首先想到的就是数组。

如前所述,在程序中引入数据结构的唯一方法是通过数组来实现。树、堆栈、队列、链表是定义数据存储方式的一些数据结构。要实现这些数据结构,数组是唯一也是最好的选择。除了其功能之外,它还非常易于使用。此外,我们需要它,因为它为我们提供了减少内存使用的机会。使用数组的程序开发人员总是非常高效并且消耗更少的内存。

谁是学习 PHP 数组的合适受众?

任何愿意学习PHP的人都是学习PHP数组的完美受众。学习数组所需要的只是了解编程基础知识。因此,更具体地说,任何了解编码基础知识的人都能够顺利理解数组。

学习数组将如何帮助您的职业发展?

由于现在需要处理大量数据,以系统的方式存储数据变得非常重要,以便可以在需要时获取数据,并且这就是数组的准确用武之地。支持您作为开发人员的职业生涯,它还将为您提供将您的职业发展到数据结构技术的机会

结论

在任何编程语言中,数组都起着非常重要的作用,因此在开发复杂程序时大部分时间都需要使用数组。它提供了一种有效的内存利用机制,使其成为处理大量数据时最需要的东西。

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

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!