首页  >  文章  >  后端开发  >  PHP 中的关联数组 

PHP 中的关联数组 

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

数组是相似和不相似数据类型的集合。数组存储与变量相关的数据。我们需要数组来创建这么多数量的变量值并将其存储在一个变量中。 PHP 中的数组分为三种类型。数值数组、关联数组和多维数组。关联数组采用键值对的形式,其中键是数组的索引,值是数组的元素。这里的键可以是用户定义的。它与数值数组类似,但键和值以键值对的形式存储。

在本主题中,我们将学习 PHP 中的关联数组。

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

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

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

语法

//First Way
$input = array("key1"=>"value1", "key2"=>"value2", "key3"=>"value3");
//Second Way
$input["key1"] = value1;
$input["key2"] = value2;
$input["key3"] = value3;

其中 $input 是数组名称,key1 是数组元素的索引,value1 是数组元素的值

如何在 PHP 中创建关联数组?

关联数组是使用 array 关键字声明的。数组中的键值使用“=>”箭头声明。有两种方法可以创建关联数组。

以下是示例。

代码:

<?php
// create associative array
// first way
$family = array("father" => "Mohan", "mother"=>"Sita", "son"=> "Raj" ,"daughter"=> "Mona");
// second way
$family["father"] = "Mohan";
$family["mother"] = "Sita";
$family["son"] = "Raj";
$family["daughter"] = "Mona";
?>

如何使用各种方法在 PHP 中遍历关联数组

我们可以通过两种方法来遍历关联数组。一是foreach循环,二是for循环。

方法#1

在此示例中,声明了一个数组并将其命名为 $family。该数组采用键值形式,其中键是父亲、母亲、儿子、女儿等关系的名称。每个键都保存关系的名称,例如索引“父亲”的第一个值是 Mohan,索引“母亲”的第二个值是 Sita,索引“儿子”的第三个值是 Raj,索引“女儿”的第四个值是 Mona。为了遍历这个数组,我们使用 foreach 循环,在该循环中,我们将键打印为 Father、Mother、Son、Daughter,将值打印为数组的 Mohan、Sita、Raj 和 Mona。

代码:

<?php
//example of the associative array
$family = array("father" => "Mohan", "mother"=>"Sita", "son"=> "Raj" ,"daughter"=> "Mona");
//first method to traverse the associative array
foreach($family as $key=>$value) {
echo $key .' is '.$value;
echo '<br>';
}
?>

方法#2

在此示例中,我们将使用与上一个示例相同的数组族,并使用 for 循环进行遍历。另外,我们将使用 array_keys 函数来获取数组的键,即父亲、母亲、儿子和女儿。这些键以数组的形式返回。 array_keys 函数采用输入数组作为参数并输出索引数组。现在为了迭代这个循环,我们将使用 for 循环并根据需要打印键和值。

代码:

<strong> </strong><?php
// Example to demonstrate for loop
$family = array("father" => "Mohan", "mother"=>"Sita", "son"=> "Raj" ,"daughter"=> "Mona");
$length = count($family);
$keys = array_keys($family);
// for loop to traverse associative array
for($i=0; $i<$length; $i++) {
echo "<br>". $keys[$i] . " => " . $family[$keys[$i]];
}
?>

输出:

PHP 中的关联数组 

PHP 中关联数组的优点

  1. 有不同的函数可用于合并两个关联数组。
  2. 类似于用户列表、堆栈、队列等
  3. 使用数组中的索引,有助于记忆数据
  4. 这些索引是用户定义的,可以进行相应更改。
  5. 超全局数组如 $_POST、$_GET、$_SESSION 数组也支持关联数组。

PHP 中关联数组按值排序

关联数组可以通过基于键和基于值两种方式进行排序。在这里,我们将学习如何按值对关联数组进行排序。有两个内置的 php 函数,如 asort() 和 asort(),它们用于按字母顺序按值对关联数组进行排序。

让我们通过一个例子来学习一下。

1。 asort(): 根据值升序对关联数组进行排序

代码:

<?php
// example to demonstrate asort() function on associative array by value in ascending order
$family = array("father" => "Mohan", "mother"=>"Sita", "son"=> "Raj" ,"daughter"=> "Mona");
print_r($family);
asort($family);
echo "<br>";
print_r($family);
?>

输出:

PHP 中的关联数组 

2。 arsort(): 根据值降序对关联数组进行排序

代码:

<?php
// example to demonstrate asort() function on associative array by value in descending order
$family = array("father" => "Mohan", "mother"=>"Sita", "son"=> "Raj" ,"daughter"=> "Mona");
echo "<br>Before Sort";
print_r($family);
arsort($family);
echo "<br>After Sort";
print_r($family);
?>

输出:

PHP 中的关联数组 

PHP 中关联数组按键排序

关联数组可以按值升序排序。以类似的方式,关联数组可以按字母顺序按升序和降序对关联数组进行排序,如下例所示。

1。 ksort(): 根据键升序对关联数组进行排序

代码:

<?php
// example to demonstrate ksort() function on associative array by key in ascending order
$family = array("father" => "Mohan", "mother"=>"Sita", "son"=> "Raj" ,"daughter"=> "Mona");
echo "<br>Before Sort";
print_r($family);
ksort($family);
echo "<br>After Sort";
print_r($family);
?>

输出:

PHP 中的关联数组 

2. krsort(): performs a sort on associative array according to the key in descending order

Code:

<?php
// example to demonstrate krsort() function on associative array by key in descending order
$family = array("father" => "Mohan", "mother"=>"Sita", "son"=> "Raj" ,"daughter"=> "Mona");
echo "<br>Before Sort";
print_r($family);
krsort($family);
echo "<br>After Sort";
print_r($family);
?>

Output:

PHP 中的关联数组 

Conclusion

Programs starting from basic like the syntax, creating the array, and traverse through the array are explained. Also, topics like advantages of the associative array and how to perform sorting on the associative array are also mentioned.

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

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