Detailed explanation of PHP arrays

【Foreword】

      This article mainly explains the knowledge points related to arrays in PHP. For the basic knowledge of PHP, I have made a summary in the previous article PHP Basic Tutorial Detailed Explanation

 

【Introduction】

   Simple understanding, an array is a simple list of number/value pairs

   In PHP, the array() function is used to create arrays, and there are three types of arrays:

① Indexed Arrays - Arrays with numeric indices

②Associative Array - Array with specified key

③ Multidimensional array - an array containing one or more arrays

 

【List】

(1) Index array

         1. Create; 2. Traverse

(2) Associative array

         1. Create; 2. Traverse

(3) Multidimensional array

         1. Introduction; 2. Two-dimensional array (element acquisition; traversal)

(4) Get the length of the array

(5) Array sorting   

 

【main body】

(1) Index array

   1. Create (2 ways):

     ①Automatically assign indexes

$array = array("one","two","three")
     ② Manually assign indexes
$array[0] = "one";
$array[1] = "two";
$array[2] = "three";

    2. Traverse

      To iterate over and output all the values ​​of an indexed array, you can use a for loop:

$array[0] = "one";
$array[1] = "two";
$array[2] = "three";
$arrLength=count($array);
for($x=0;$x<$arrLength;$x++) {
  echo $array[$x]."<br>";
}

 

(2) Associative array

   1. Create (2 ways):

       ① Uniform distribution of key values

$array = array("one"=>"1","two"=>"2","three"=>"3");

       ②Assign key values ​​one by one

$array["one"] = "1";
$array["two"] = "2";
$array["three"] = "3";

    2. 遍历

      如需遍历关联数组的所有值,可以使用 foreach 循环:

$array['one'] = "1";
$array['two'] = "2";
$array['three'] = "3";
foreach($array as $x=>$value){
    echo "键是".$x."---"."值是".$value."<br>";
}

 

(3)多维数组

   1. 简介:与JS里相同,多维数组指的是包含一个或多个数组的数组。选取元素上,几维数组就需要几个索引,例如对于二维数组,需要两个索引来选取元素;对于三维数组,需要三个索引来选取元素等等

   2. 二维数组

   假设这个两维数组包含了四个数组,并且它有两个索引(下标):行和列。

    ①元素获取

    如需访问 $cars 数组中的元素,必须使用两个索引(行和列):

<?php
$cars = array(
   array("Volvo",33,20),
   array("BMW",17,15),
   array("Saab",5,2),
   array("Land Rover",15,11)
   );
echo $cars[0][0].": 库存:".$cars[0][1].", 已售:".$cars[0][2].".<br>";
echo $cars[1][0].": 库存:".$cars[1][1].", 已售:".$cars[1][2].".<br>";
echo $cars[2][0].": 库存:".$cars[2][1].", 已售:".$cars[2][2].".<br>";
echo $cars[3][0].": 库存:".$cars[3][1].", 已售:".$cars[3][2].".<br>";
?>

    ②遍历

   可以在 For 循环中使用另一个 For 循环,来获得 $cars 数组中的元素(我们仍需使用两个索引):

<?php
$cars = array(
   array("Volvo",33,20),
   array("BMW",17,15),
   array("Saab",5,2),
   array("Land Rover",15,11)
   );
   
for ($row = 0; $row <  4; $row++) {
   echo "<p><b>行数 $row</b></p>";
   echo "<ul>";
   for ($col = 0; $col <  3; $col++) {
     echo "<li>".$cars[$row][$col]."</li>";
   }
   echo "</ul>";
}
?>

 

(4)获取数组长度

当我们遍历索引数组时,需要获取数组长度,此时需要用到count()函数

count() 函数用于返回数组的长度(元素数),案例:

<?php
$cars=array("Volvo","BMW","SAAB");
echo count($cars);
?>

 

(5)数组排序

     数组中的元素能够以字母或数字顺序进行升序或降序排序

     数组排序函数:

              ①sort() - 以升序对数组排序

              ②rsort() - 以降序对数组排序

              ③ksort() - 根据键,以升序对关联数组进行排序

              ④krsort() - 根据键,以降序对关联数组进行排序

              ⑤asort() - 根据值,以升序对关联数组进行排序

              ⑥arsort() - 根据值,以降序对关联数组进行排序

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326177300&siteId=291194637