PHP 中的数组
下面的文章《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.数值数组
在此数组类型中,索引始终是数字,它不能是字符串。相反,它可以存储任意数量的元素和任意类型的元素。
语法:
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 //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); ?>
输出:
现在我们将学习如何使用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>"; } ?>
输出:
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>";} ?>
输出:
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 //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 中数组的方法:
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); ?>
输出:
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:
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:
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:
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:
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:
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:
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:
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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

如果您是一位经验丰富的 PHP 开发人员,您可能会感觉您已经在那里并且已经完成了。您已经开发了大量的应用程序,调试了数百万行代码,并调整了一堆脚本来实现操作

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。
