1. 首页
  2. php

PHP – 数组

数组是一种数据结构,它将一个或多个相似类型的值存储在单个值中。例如,如果要存储100个数字,而不是定义100个变量,则很容易定义100个长度的数组。

PHP - 数组

有三种不同类型的数组,每个数组值使用ID c访问,ID c称为数组索引。

  • 数值数组 – 具有数字索引的数组。以线性方式存储和访问值。
  • 关联数组 – 以字符串作为索引的数组。这将元素值与键值相关联而不是以严格的线性索引顺序存储。
  • 多维数组 – 使用多个索引访问包含一个或多个数组和值的数组

 – 内置数组函数在函数参考PHP数组函数中给出

数字数组

这些数组可以存储数字,字符串和任何对象,但它们的索引将由数字表示。默认情况下,数组索引从零开始。

以下是显示如何创建和访问数字数组的示例。

这里我们使用array()函数来创建数组。该功能在功能参考中说明。

<html>
   <body>
   
      <?php
         /* First method to create array. */
         $numbers = array( 1, 2, 3, 4, 5);
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
         
         /* Second method to create array. */
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
      ?>
      
   </body>
</html>

这将产生以下结果 –

Value is 1 
Value is 2 
Value is 3 
Value is 4 
Value is 5 
Value is one 
Value is two 
Value is three 
Value is four 
Value is five 

关联数组

关联数组在功能方面与数字数组非常相似,但它们的索引不同。关联数组将其索引作为字符串,以便您可以在键和值之间建立强关联。

要将员工的工资存储在数组中,数字索引的数组将不是最佳选择。相反,我们可以使用员工姓名作为关联数组中的键,值将是他们各自的薪水。

注意 – 打印时不要将关联数组保留在双引号内,否则不会返回任何值。

<html>
   <body>
      
      <?php
         /* First method to associate create array. */
         $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
         
         echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
         echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
         echo "Salary of zara is ".  $salaries['zara']. "<br />";
         
         /* Second method to create array. */
         $salaries['mohammad'] = "high";
         $salaries['qadir'] = "medium";
         $salaries['zara'] = "low";
         
         echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
         echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
         echo "Salary of zara is ".  $salaries['zara']. "<br />";
      ?>
   
   </body>
</html>

这将产生以下结果 –

Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low

多维数组

多维数组中主数组中的每个元素也可以是数组。子数组中的每个元素都可以是数组,依此类推。使用多个索引访问多维数组中的值。

在这个例子中,我们创建了一个二维数组来存储三个学科的三个学生的标记 –

此示例是关联数组,您可以以相同的方式创建数字数组。

<html>
   <body>
      
      <?php
         $marks = array( 
            "mohammad" => array (
               "physics" => 35,
               "maths" => 30,	
               "chemistry" => 39
            ),
            
            "qadir" => array (
               "physics" => 30,
               "maths" => 32,
               "chemistry" => 29
            ),
            
            "zara" => array (
               "physics" => 31,
               "maths" => 22,
               "chemistry" => 39
            )
         );
         
         /* Accessing multi-dimensional array values */
         echo "Marks for mohammad in physics : " ;
         echo $marks['mohammad']['physics'] . "<br />"; 
         
         echo "Marks for qadir in maths : ";
         echo $marks['qadir']['maths'] . "<br />"; 
         
         echo "Marks for zara in chemistry : " ;
         echo $marks['zara']['chemistry'] . "<br />"; 
      ?>
   
   </body>
</html>

这将产生以下结果 –

Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39

原创文章,作者:零久SEO,如若转载,请注明出处:http://www.seo0j.com/317.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论列表(12条)

  • yeezy boost 350 2020年2月24日 上午1:05

    I simply wished to appreciate you once more. I do not know the things I would’ve implemented in the absence of the type of points provided by you on this situation. Entirely was a very terrifying matter in my view, however , noticing your well-written way you solved the issue took me to cry for joy. I’m grateful for the assistance and even trust you realize what a powerful job you happen to be getting into training the others through the use of your website. I am certain you’ve never got to know any of us.

  • yeezy 2020年2月26日 上午10:12

    Needed to put you the little remark to be able to say thank you as before for those pleasant knowledge you’ve documented above. It’s certainly surprisingly open-handed of you to give unreservedly exactly what many individuals would’ve marketed as an e book to help with making some cash on their own, and in particular considering the fact that you might well have done it in the event you wanted. These advice as well worked to become a fantastic way to be aware that other individuals have similar interest like my own to see a little more on the subject of this issue. I am sure there are millions of more fun moments ahead for individuals who browse through your website.

  • huarache shoes 2020年2月29日 上午5:34

    I simply wanted to thank you so much all over again. I’m not certain the things I could possibly have done without the entire strategies discussed by you concerning my subject matter. This was a very intimidating setting for me, nevertheless coming across the specialised way you dealt with that forced me to leap with contentment. I will be thankful for this support and as well , hope you realize what a great job your are providing instructing the mediocre ones through your website. I am sure you haven’t got to know all of us.

  • supreme clothing 2020年3月2日 下午5:49

    I simply wanted to write a small note in order to say thanks to you for all the unique tactics you are placing on this site. My extensive internet look up has at the end been compensated with reputable content to write about with my pals. I ‘d mention that most of us readers are undoubtedly endowed to dwell in a magnificent community with so many special people with good solutions. I feel rather fortunate to have discovered the website page and look forward to some more exciting moments reading here. Thanks once again for all the details.

  • supreme clothing 2020年3月5日 上午8:17

    I simply desired to thank you so much all over again. I do not know the things I could possibly have used in the absence of the type of suggestions shown by you directly on such situation. Certainly was the troublesome difficulty in my opinion, but looking at the very skilled tactic you managed that made me to weep with delight. I’m happy for the support as well as believe you know what a great job that you’re providing teaching others all through a web site. I am sure you’ve never come across all of us.

  • air max 97 2020年3月8日 上午3:04

    My husband and i ended up being so fortunate Jordan could carry out his survey using the ideas he got when using the web pages. It is now and again perplexing to simply happen to be freely giving guidance which usually the others could have been trying to sell. And we take into account we now have you to be grateful to for that. These explanations you have made, the straightforward site navigation, the friendships you aid to engender – it is everything exceptional, and it’s really making our son and us feel that this subject matter is thrilling, which is truly mandatory. Many thanks for everything!

  • golden goose sneakers 2020年3月10日 上午10:01

    I’m just writing to let you be aware of of the magnificent experience my wife’s princess enjoyed checking the blog. She came to understand some pieces, which included what it is like to have an incredible helping character to make many people without problems thoroughly grasp several specialized topics. You undoubtedly surpassed visitors’ expected results. Thank you for giving such insightful, trustworthy, educational and as well as cool tips about this topic to Jane.

  • goyard handbags 2020年3月17日 上午2:59

    I am also writing to make you understand of the fantastic discovery my cousin’s girl undergone studying your web site. She discovered numerous pieces, with the inclusion of how it is like to possess a wonderful teaching nature to get many people very easily fully understand some extremely tough matters. You truly did more than our own desires. Thank you for distributing such beneficial, trusted, explanatory not to mention unique tips on this topic to Gloria.

  • nike react 2020年3月19日 上午10:01

    I have to voice my respect for your kindness for all those that must have assistance with this content. Your very own commitment to getting the message all-around became exceptionally advantageous and have all the time permitted people like me to achieve their aims. Your entire warm and friendly help and advice indicates a whole lot to me and especially to my peers. Warm regards; from everyone of us.

  • jordans 2020年3月22日 上午7:17

    A lot of thanks for all of your work on this blog. My mom really likes going through investigations and it is simple to grasp why. Most of us hear all of the compelling medium you offer great tactics by means of your website and even increase contribution from people about this issue so my child is now becoming educated a whole lot. Enjoy the rest of the year. You’re the one doing a tremendous job.

  • curry 7 shoes 2020年3月25日 上午3:57

    I simply needed to say thanks once more. I’m not certain what I could possibly have gone through without those concepts shown by you directly on this theme. It truly was a real distressing difficulty for me personally, but being able to see the very specialised manner you dealt with the issue made me to leap over joy. Extremely grateful for the assistance and hope you are aware of an amazing job that you’re accomplishing instructing the mediocre ones via a site. Most probably you’ve never got to know all of us.

  • supreme clothing 2020年3月27日 下午12:19

    I not to mention my buddies appeared to be reading through the great key points located on the blog and so all of a sudden got a horrible feeling I never thanked the web site owner for those techniques. These young men became excited to read through all of them and have now extremely been using those things. Thanks for turning out to be well helpful and then for pick out certain essential ideas most people are really eager to learn about. Our honest apologies for not expressing appreciation to earlier.

联系我们

15555333984

在线咨询:点击这里给我发消息

邮件:lxa6@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code