首页  >  文章  >  后端开发  >  PHP 数组函数

PHP 数组函数

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

PHP 数组函数(超文本预处理器的缩写)是一种广泛使用的通用脚本语言;它与 HTML 和 Web 开发的兼容性使其成为易于理解的关键技术。 PHP 中的数组是指可以在单个

中保存或存储多个值的变量类型

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

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

可以轻松嵌入到HTML中,让我们用简述来见证 –

代码:

<!DOCTYPE html>
<html>
<head>
<title>……………</title>
</head>
<body>
<?php
echo  "Hello, I am a PHP !";
?>
</body>
</html>

输出:

PHP 数组函数

上面的脚本文件非常清楚地证明了 PHP 脚本与 HTML 的兼容性有多好。 PHP 代码包含特殊的开始和结束括号。

如何在 PHP 中创建数组?

数组()

下面我们列出了数组的工作原理 –

$color = array("red", "green", "blue");

输出

$color[0] = “红色”
$color[1] = “绿色”
$color[2] = “蓝色”

这里的目的是将颜色名称存储在一个颜色变量中。所以我们在数组函数中有一个颜色变量,在这个函数中,我们以字符串格式一一命名了所有颜色。

PHP 数组函数

有 3 种不同类型的数组:

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

这三个解释如下:

1.数值数组

数字数组是那些具有数字索引的数组。让我们看看数值数组的语法 - 有两种类型的语法。

第一种方式:

$array_name[0] = value;

第二种方式:

$array_name[] = value;
注意:这里方括号[0]中的零代表索引号。

值表示用户想要在数组中存储的内容。

第一种和第二种语法有一些区别,一种是 [] 中为零,另一种是 [] 为空白。

默认情况下,所有数组都以索引 0 开头,这意味着对于第一个数组,如果我们在 [] 中输入 0 或将其留空 [] 都表示相同的意思。再看一个例子以更好地理解差异

$array_name[] = value; {either you put 0 or leave it blank – both means same}
$array_name [1] = value;

下面列出了具有不同值和不同索引的数组 –

$name[0] = "Alex";
$name[1] = "Peter";
$name[2] = "Lucy"

2.关联数组

关联数组是以字符串作为索引的数组。存储的值是与键值关联执行的,而不是线性索引。

让我们看看关联数组的语法。

$array_name["key"] = value;
注意:将其称为键或将其称为索引(两者具有相同的含义)。

当您必须在值和键(或索引)之间创建关系时,请使用关联数组。

3.多维数组

多维数组是包含一个或多个数组以及其中的值的数组。这些数组可以通过多个索引来访问。

在单一定义中,我们可以将多维称为数组的数组。多维数组可以是 1D(I 维)、2D(2 维)…….n 维。

Alex England 23
Peter Germany 26
Lucy Holland 27

因此,如果我们以 2D 形式存储,分配将是下面列出的内容 –

Alex [0][0] England[0][1] 23[0][2]
Peter[1][0] Germany[1][1] 26[1][2]
Lucy[2][0] Holland[2][1] 27[2][2]

The same goes for ‘n’ number of dimensions and allocations.

Examples on Types of the Array Function

Let us see the types of the array with the help of an example:

1. Numeric Array

Code:

<html>
<body>
<?php
$numbers[] = "eleven";
$numbers[] = "twelve";
$numbers[] = "thirteen";
$numbers[] = "fourteen";
$numbers[] = "fifteen";
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
?>
</body>
</html>

Output:

PHP 数组函数

2. Associative Array

Code:

<html>
<body>
<?php
$salaries['Alex'] = "high";
$salaries['Peter'] = "medium";
$salaries['Lucy'] = "low";
echo "Salary of Alex is ". $salaries['Alex'] . "<br />";
echo "Salary of Peter is ". $salaries['Peter']. "<br />";
echo "Salary of Lucy is ". $salaries['Lucy']. "<br />";
?>
</body>
</html>

Output:

PHP 数组函数

3. Multidimensional array

Code:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Multidimensional Array</title>
</head>
<body>
<?php
// Define nested array
$contacts = array(
array(
"name" => "Petergomes",
"email" => "[email protected]",
),
array(
"name" => "Clark anthony",
"email" => "[email protected]",
),
array(
"name" => "lucy disilva",
"email" => "[email protected]",
)
);
// Access nested value
echo "Peter gomes's Email-id is: " . $contacts[0]["email"];
?>
</body>
</html>

Output:

PHP 数组函数

Advantages

Following are some of the advantages described.

  • When your intention is to represent multiple data that belong to the same type with using only single indexing naming.
  • It has wide applicability as it can be used to implement other data structures like stacks, trees, queues, graphs, and linked lists.
  • 2D/3D arrays are used to represent matrices effectively
  • It has less coding with the elimination of complexity
  • Sorting can be done easily

Conclusion

PHP arrays hold crucial importance in PHP programming, it acts as the ultimate variable of PHP. It behaves as a storage container for collecting elements. Arrays also can store other variables within like strings, integers, and even other arrays. If you have to deal with an unknown amount of variables you must prefer to work using arrays. Loops can be used to output values in arrays, also by simply calling specific elements with the index or key values.

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

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