首页  >  文章  >  后端开发  >  PHP 中的索引数组

PHP 中的索引数组

WBOY
WBOY原创
2024-08-29 12:44:37999浏览

在下面的文章中,我们将讨论 PHP 中的索引数组。数组是一种数据结构,或者更像是上面讨论的存储位置,它以单个名称存储一个或多个相同类型的数据。理解这一点的另一种方法是,我们拥有结构数组中每个值的键,因此当我们的单个变量保存项目或值的列表时,我们可以使用这些键来识别它们中的每一个。这种数据结构的优化可以用作数组、字典或值的集合、堆栈队列等。并且由于数组内的值也可以是数组本身,因此我们有可能创建一棵树或多维数组。

数组类型

现在,根据数组中使用的键的大小和类型(可以是字符串或整数),我们主要创建三种类型的数组。可以创建任何类型的值。因此,类型可以创建为数字或索引数组、关联数组和多维数组。

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

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

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

PHP 中的索引数组

  1. 索引或数字数组:我们的键本质上是数字的类型,即数字索引。这里的值是线性访问并以线性方式存储的。
  2. 关联数组:这种类型的数组以字符串作为访问值的键,并且其键和值之间存在强关联。它类似于索引数组。
  3. 多维数组:多维数组可以包含一个数组,该数组内部可能包含另一个数组,依此类推。在多维数组中,使用不同的索引来访问值。

索引数组

正如上面简要讨论的,索引数组是一种通过数字索引来访问其值的数组类型。但是,它们可以存储数字、字符、字符串等。默认情况下,如果不指定,数组索引将用数字表示,从索引 0 开始,以索引 -1 结束.

创建索引数组主要有两种方法。

  1. 第一个是简单地为每个值“手动”分配一个索引并创建数组。
  2. 第二个,我们可以使用不带任何索引的array()函数,索引将默认分配,并且第一个元素或值从0开始。我个人比较喜欢这个方法。

让我们一一看看创建数组的两种方法。

手动索引分配:在下面给出的示例中,我们手动将索引一一分配给我们的值。

<?php
$employee[0] = "Ram";
$employee[1] = "Male";
$employee[2] = "28";
echo "My name is ".$employee[0].", I am ".$employee[2] . "  years old and my gender is ".$employee[1].".";
?>

上面的示例代码将产生如下输出:

PHP 中的索引数组

您还可以在下面给出的程序截图及其在实时环境中的输出中看到相同的代码。

函数 array(): 下面编写的代码使用 array() 函数创建一个名为 $autos 的索引数组。该函数正在将三个元素分配给我们的数组名称。

然后,我们形成一个包含数组值的简单文本行,并使用 echo 语句打印它们。

代码:

<?php
$employee = array("Ram", "Male", "28");
echo "My name is ".$employee[0].", I am ".$employee[2] . "  years old and my gender is ".$employee[1].".";
?>

输出:

PHP 中的索引数组

注意:我们首先访问$employee[2]索引,然后根据需要调用$employee[1]。

但是如果我的数组中有几十个值并且我需要打印它们怎么办?

使用带有 echo 语句的分隔符输入数组中的所有值来打印所有值会很麻烦。为此,一个简单的方法是我们可以遍历完整的数组并能够打印值。在索引数组中遍历索引数组是简单容易的;在这里,我们使用循环。

在 PHP 中遍历索引数组

遍历数组意味着将数组的值一一读取,并在需要时进行打印。可以轻松遍历索引数组;我们简单地使用“循环遍历值”方法。我们将使用 for 循环或 foreach 循环来遍历索引数组,然后打印所有所需的值。

代码:

<?php
$employee = array("Ram", "Male", "28");
$length = count($employee);
for($x = 0; $x < $length; $x++)
{
echo $employee[$x];
echo "<br/>";
}
?>

输出:

PHP 中的索引数组

The above program prints the contents of our array.

Note: The values Ram, Male and 28 are printed in new lines because of the break statement (
) we used in our code.

Code:

<?php
$employee = array("Ram", "Male", "28");
foreach($employee as $e)
{
echo "$e <br/>";
}
?>

Output:

PHP 中的索引数组

You can see the above simple code and its output in the live environment in the following screenshot.

Another commonly used method in arrays is to fetch the length of the array. The count() function is used for this purpose. Following is a simple PHP code creating an array and then returning its length. We used the count() function, which is returning the length, i.e. the number of elements our array contains, as shown in the output.

Code:

<?php
$employee = array("Ram", "Male", "28");
echo count($employee);
?>

Output:

PHP 中的索引数组

The output is 3 (see in the above screenshot), which is equal to the total number of elements or values in our array $employee.

Conclusion

In simple words, arrays tend to show special characteristics with a capacity to store several values in one variable. They are quite stretchable; that is, if one needs to add more values afterward, it can be done with ease. An indexed array is a smarter way to bind all related information together, for example, an employee’s details. It also helps in writing clean code.

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

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