PHP 中的数组

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

下面的文章《PHP 中的数组》提供了在 PHP 中创建数组的概述。数组是类似数据类型的集合。数组在单个变量中存储多个值。当变量也可以存储值时,为什么需要数组呢?答案是因为存储有限数据的值(例如数字5的计数)是可能的,但是当计数增加到例如100或200时,我们需要在100个变量中存储100个值,这有点困难;因此,我们将其存储在一个数组中。这就是使用数组的原因。

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

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

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

如何在 PHP 中创建数组?

语法:

variablename = array();
登录后复制

或者

variablename[i] = value;
登录后复制

其中变量名是变量的名称,i是键,或者索引值是元素值。

创建数组的示例

$colors = array("Red","Green","Blue");
登录后复制

为了计算数组的长度,我们使用 count 关键字。

$length = count($colors); // output is 3
登录后复制

数组中的每个值称为数组的一个元素。数组索引从0开始。数组中最后一个元素的索引是数组总长度减1。

在上面给出的示例中,Red 的索引为 0,Green 为 1,Blue 为 2。因此,借助索引或键访问数组变得更容易。为了获取数组每个索引处的值,我们循环遍历给定的数组。要循环数组,我们使用 foreach 循环或 for 循环。

数组在 PHP 中如何工作?

像 foreach 和 for 这样的循环用于循环遍历数组。每个数组都有从 0 开始的索引,依此类推:

PHP 中的数组类型

PHP中有三种类型的数组;让我们详细了解每种类型的数组:

  1. 数字或索引数组
  2. 关联数组
  3. 多维数组
1.数值数组

在此数组类型中,索引始终是数字,它不能是字符串。相反,它可以存储任意数量的元素和任意类型的元素。

语法:

variable name = array("value1","value2","value3","value4")
登录后复制

代码:

<?php
//Example to demonstrate numeric array
$input = array("Apple", "Orange", "Banana", "Kiwi");
//Here, to get these values we will write like
echo $input[0] . "\n"; // will give Apple
echo $input[1] . "\n"; // will give Orange
echo $input[2] . "\n"; // will give Banana
echo $input[3] . "\n"; // will give Kiwi
// To get the length of array we will use count
echo "The count of the array is " . count($input); // will give 4
echo "\n";
//To print the array we can use
print_r($input);
?>
登录后复制

输出:

PHP 中的数组

或者

声明数值数组的另一种方法是以下程序。在这个程序中,我们还将看到修改和打印值。

代码:

<?php
//Example to demonstrate numeric array in another way
$input[0] = "Apple";
$input[1] = "Orange";
$input[2] = "Banana";
$input[3] = "Kiwi";
// To get Kiwi we will write like
echo $input[3]."<br>"; // will give Kiwi
//To modify Orange value
$input[1] = "Mango";
// Now echo $input[1] will give Mango
echo $input[1]."<br>"; // Mango
//To print the array we can use
print_r($input);
?>
登录后复制

输出:

PHP 中的数组

现在我们将学习如何使用for循环来遍历数组

代码:

<?php
//Example to demonstrate for loop on a numeric array
//declaring the array
$input = array("Apple", "Orange", "Banana", "Kiwi", "Mango");
//the for loop to traverse through the input array
for($i=0;$i<count($input); $i++) {
echo $input[$i];
echo "<br>";
}
?>
登录后复制

输出:

PHP 中的数组

2.关联数组

这个数组是键值对的形式,其中键是数组的索引,值是数组的元素。

语法:

$input = array("key1"=>"value1",
"key2"=>"value2",
"key3"=>"value3",
"key4"=>"value4");
登录后复制

或者

不使用数组关键字声明关联数组的另一种方法

$input[$key1] = $value1;
$input[$key2] = $value2;
$input[$key3] = $value3;
$input[$key4] = $value4;
登录后复制

代码:

<?php
//Example to demonstrate associative array
//declaring an array
$input = array(
"Jan"=>31,
"Feb"=>28,
"Mar"=>31,
"Apr"=>30);
// the for loop to traverse through the input array
foreach($input as $in) {
echo $in."<br>";}
?>
登录后复制

输出:

PHP 中的数组

3.多维数组

这个数组是数组的数组,其中数组的值包含数组。

语法:

$input =array(
array('value1', 'value2', 'value3'),
array('value4', 'value5', 'value6'),
array('value7', 'value8', 'value9'));,
登录后复制

代码:

<?php
//Example to demonstrate multidimensional array
// declaring a multidimensional array
$input = array ("colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
//the foreach loop to traverse through the input array
foreach($input as $key=>$value) {
echo $key .'--'. "<br>";
foreach($value as $k=>$v)
{echo $v ." ";}
echo "<br>";
}
?>
登录后复制

输出:

PHP 中的数组

或者

关联数组中的多维数组

代码:

<?php
//Example to demonstrate multidimensional array
// declaring a 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
));
//the foreach loop to traverse through the input array
//foreach to loop the outer array
foreach($input as $book) {
echo "<br>";
// foreach to loop the inner array
foreach($book as $key=>$value)
{
echo $key." ". $value. "<br>";}
}?>
登录后复制

输出:

PHP 中的数组

PHP 中的数组方法

以下是 PHP 中数组的方法:

1. Count() 方法

该方法用于统计数组中元素的数量。

语法:

Count(array, mode)
登录后复制

需要计数的地方,模式是可选的。

代码:

<?php
//Example to demonstrate use of in_array method
//declaring associative array
$input=array('English','Hindi','Marathi');
//counting the number of elements in the given array
echo count($input);
?>
登录后复制

输出:

PHP 中的数组

2. Array_walk() 方法

该方法接受两个参数作为输入;第一个参数是输入数组,第二个参数是声明的函数的名称。该方法用于循环遍历数组中的每个元素。

语法:

array_walk(array, function_name, parameter...)
登录后复制

其中需要数组的地方需要 function_name

参数是可选的

代码:

<?php
//Example to demonstrate use of array_walk method
//creating a function to print the key and values of the given array
function fun($val, $k) {
echo $k. " --" .$val ."\n";
}
// declaring associative array
$input=array("e"=>'English', "h"=>'Hindi', "m"=>'Marathi');
//passing this array as a first parameter to the function
// array_walk,
//second paramter as the name of the function being called
array_walk($input,"fun");
?>
登录后复制

Output:

PHP 中的数组

3. In_array() method

This method performs a search on the array, whether the given array contains a particular value or not. If found or not found, it will execute respective if, else block

Syntax:

in_array(search_value, array_name)
登录后复制

Where both the parameters are required

Code:

<?php
//Example to demonstrate use of in_array method
// declaring associative array
$input=array('English','Hindi','Marathi', "Maths", "Social Science");
// using in_array to find Maths in given array
if(in_array("Maths", $input)) {
echo "Found Maths in the given array";
}
else
{
echo "Did not find Maths in the given array";
}
?>
登录后复制

Output:

PHP 中的数组

4. Array_pop() method

This method removes the last element from the given array.

Syntax

array_pop(array_name)
登录后复制

Code:

<?php
//Example to demonstrate use of array_pop method
// declaring array
$input=array('English','Hindi','Marathi');
// before using array_pop on the given array
print_r($input);
// after using array_pop method on the given array
array_pop($input);
echo "\n ";
print_r($input);
?>
登录后复制

Output:

PHP 中的数组

5. Array_push() method

This method adds given elements at the end of the array.

Syntax:

array_push(array_name, value1, value2, ...)
登录后复制

Code:

<?php
//Example to demonstrate use of array_push method
// declaring array
$input=array('English','Hindi','Marathi');
// before using array_push on the given array
print_r($input);
// after using array_push method on the given array
array_push($input, "Economics", "Maths", "Social Science");
echo "\n";
//printing the array
print_r($input);
?>
登录后复制

Output:

PHP 中的数组

6. Array_shift() method

This method removes and returns the first element of the array.

Syntax: 

array_shift(array_name)
登录后复制

Code:

<?php
//Example to demonstrate use of array_push method
// declaring array
$input=array('English','Hindi','Marathi');
// before using array_shift on the given array
print_r($input);
echo "\n";
// after using array_shift method on the given array
echo array_shift($input);
?>
登录后复制

Output:

PHP 中的数组

7. Array_unshift() method

This method inserts given elements into the beginning of the array.

Syntax:

array_unshift(array_name, value1, value2,…)
登录后复制

Code:

<?php
//Example to demonstrate use of array_push method
// declaring array
$input=array('English','Hindi','Marathi');
// before using array_unshift on the given arrayprint_r($input);
echo "\n";
// after using array_unshift method on the given array
array_unshift($input, "Economics");
print_r($input);
?>
登录后复制

Output:

PHP 中的数组

8. Array_reverse() method

This method is used to reverse the elements of the array.

Syntax:

array_reverse(array_name, preserve)
登录后复制

where array_name is required,

preserve is optional

Code:

<?php
//Example to demonstrate use of in_array method
// declaring associative array
$input=array("e"=>'English',"h"=>'Hindi',"m"=>'Marathi');
// array before reversing the elements
print_r($input);
echo "\n";
// printing the reverse
// array after reversing the elements
print_r(array_reverse($input));
?>
登录后复制

Output:

PHP 中的数组

Conclusion

This article covers all levels of concepts, simple and complex, of the topic arrays in PHP. I hope you found this article interesting and informative for the learning purpose.

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

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