PHP Basics Tutorial 8: Array

黄舟
Release: 2023-03-06 08:54:01
Original
1417 people have browsed it

Explanation in this section

  • Introduction to arrays

  • Creation of arrays

  • Dynamic growth of array

  • Traversal of array

  • Related functions of array

  • Array Operator

Preface

What we have learned before is Variables and constants are individual pieces of data, and sometimes we have such a need to store some values ​​of the same type or some irrelevant values ​​reasonably, and when using them, they can be used according to certain rules Get the data. At this time, the previous variables cannot meet the requirements, so arrays are used here.

Introduction to arrays

Arrays in PHP are different from arrays in other languages. This is also caused by the looseness of PHP language syntax. What's the difference?

The so-called array is a collection of elements of the same data type or different data types arranged in a certain order. It is a collection of variables that names a limited number of variables with a name and then uses numbers to distinguish them. This name is called Array name, number is called subscript. The individual variables that make up an array are called components of the array, also called elements of the array, and sometimes called subscript variables.

The popular understanding of the definition of an array is to store a collection of data.

<?php

    //定义一个数组,里面存放的数据类型可以任意类型
    $arr = array(12,true,false,&#39;abcd&#39;,12.2);
    print_r($arr);//输出数组。
.....结果......
Array ( [0] => 12 [1] => 1 [2] => [3] => abcd [4] => 12.2 )
Copy after login

You can see that various data types are stored in the array, and they can be output during output. And you can see that when outputting, each value has a corresponding subscript. This subscript is called a key, and the value is a value. When we want to get a certain value, we can Get it by key.

$a = 数组名[键名]
echo $arr[0];
.....结果......
12
Copy after login

When the key value of the array is a number, its subscript is calculated from 0.

So how is the array created?

Three ways to create arrays

  • Index arrays (subscripts are numeric) There are three ways to define arrays in PHP.

  • Associative array (the subscript is a string)

  • The subscript of the array has both.

The first one

<?php

    //定义数组,数值用逗号隔开。
    $arr = array(1,2,3,4,&#39;abcd&#39;,true);
    print_r($arr);
.....结果......
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => abcd [5] => 1 )
Copy after login

Use the array() language structure to create a new array. You can see from the output that the subscript is a number. . The data is also retrieved through numerical subscripts. This is the index array.

The second one

<?php

    //关联数组,自己定义下标,用=>来定义键和值
    $arr = array(&#39;a&#39; => 1,&#39;b&#39; => 2,&#39;c&#39; => &#39;abcde&#39;, &#39;d&#39; => true);
    print_r($arr);
.....结果......
Array ( [a] => 1 [b] => 2 [c] => abcde [d] => 1 )
Copy after login

This method is also defined using the array() syntax structure, but the subscript of the array is defined by ourselves. Here we can Define the subscript for your own situation. Regarding the key value definition, please note that the key will have the following forced conversion

  1. Strings containing legal integer values ​​will be converted to integers. For example, the key name "8" will actually be stored as 8. But "08" will not be cast because it is not a legal decimal value.

  2. Floating point numbers will also be converted to integers, which means their decimal parts will be rounded off. For example, the key name 8.7 will actually be stored as 8.

  3. Boolean values ​​will also be converted to integers. That is, the key name true will actually be stored as 1 and the key name false will be stored as 0.

  4. Null will be converted into an empty string, that is, the key name null will actually be stored as "".

  5. Arrays and objects cannot be used as keys. Insisting on doing this will result in a warning: Illegal offset type.

If multiple units use the same key name in the array definition, only the last one will be used, and the previous ones will be overwritten.

To get the value in the associative array, you need to use your own defined key to get it.

The third type

<?php

    $arr[0] = 1;
    $arr[2] = 2;
    $arr[&#39;a&#39;] = 12;
    $arr[4] = 45;

    print_r($arr); 
.....结果......
Array ( [0] => 1 [2] => 2 [a] => 12 [4] => 45 )
Copy after login

The definition of the third type of array can be defined directly using square brackets. The system will default to thinking that this is an array and create it. For subscripts you can use numbers or strings. But if multiple units use the same key name in the array definition, only the last one is used, and the previous ones are overwritten.

Dynamic growth of arrays

The length of PHP’s array is not fixed. When you finish filling in the data, you can still fill in the data. At the same time, the array The length will also increase accordingly. This is the dynamic growth of the php array.

<?php

    for($i = 0; $i < 10; $i++){
        $arr[] = $i;
    }
    print_r($arr);
......结果.......
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 )
Copy after login

The above code uses a for loop and uses the third definition method of the array to assign values. However, you can see that we did not write the subscript in the square brackets of the array, but the program did not report an error. This It's because of the dynamic growth of the array. If a key value is not specified, PHP will automatically use the largest previously used integer key plus 1 as the new key. Therefore, you can see in the output that the subscript automatically increases from 0.

Traversal of arrays

We introduced the creation and basic use of arrays earlier, so how do we remove all the values ​​​​from the array? In PHP, you can use a loop control process to get the values ​​in an array, and loop through the array to get the values ​​inside one by one.

for

You can use for to get the values ​​in the array during array traversal.

<?php

    $arr = array(1,9,4,2,8,10,5);
    $sum = count($arr);
    for($i = 0; $i < $sum; $i++){
        echo $arr[$i] . &#39;  &#39;;
    }
.....结果......
1 9 4 2 8 10 5
Copy after login

上面的count()这个系统函数是统计一个数组里的所有元素,或者一个对象里的东西。从而的到数组的大小,也就是长度。然后通过for循环,把$i当做数组的下标($i从0开始)。

foreach

在上面的代码中如果我们的数组是关联数组,也就是数组的下标是一个字符串,那么上面的代码是不可能取出来。所以我们可以使用PHP提供的另外一种循环方法:foreach()。它的语法有两种:

foreach(数组名 as $key => $value){

}

foreach(数组名 as $value){

}
Copy after login

其中“as”是一个关键字,不能变。

foreach里面有可以有三个参数,第一个参数是数组的名字,第二个参数

value是用来存储从数组中取出来的值。当然键和值得名字可以根据自己的情况自己取。

当里面的参数是两个的时候,$value只用来存放从数组中取出来的值,而没有键。

<?php

    $arr = array(1,9,4,2,8,10,5);

    foreach ($arr as $key => $value) {
        echo $key . &#39;=>&#39; . $value . &#39;  &#39;;
    }

    echo &#39;<br>&#39;;

    foreach ($arr as $value) {
        echo $value . &#39;  &#39;;
    }
.....结果......
0=>1 1=>9 2=>4 3=>2 4=>8 5=>10 6=>5 
1 9 4 2 8 10 5
Copy after login

在第二个循环中没有键的输出。

上面的两种循环结构,就是我们常用到的数组遍历的方法,其中第二个方法,也就是用foreach的方法,我们会经常用到,因为这种方法对于所有的数组都适用,而不用考虑下标的问题。

数组的相关函数

  • count($array) 统计一个数组里的所有元素,或者一个对象里的东西。从而的到数组的大小,也就是长度。然后通过for循环,把

    i从0开始)。

  • is_array($arr) 判断变量是否是一个数组。传一个变量进去,如果这个变量是一个数组,则返回true,否则则返回false;

  • array_search (value) 在数组中搜索给定的值,如果成功则返回相应的键名 。当我们想知道一个数组中有没有一个数,可以使用这个函数,如果数组中有你想找的值,则返回这个值对应的键值。

  • array_reverse($arr),传进去一个数组,返回一个与这个数组相反的数组。

    <?php
    
    $arr = array(1,9,4,2,8,10,5);
    
    $array = array_reverse($arr);
    
    print_r($array);
    .....结果......
    Array ( [0] => 5 [1] => 10 [2] => 8 [3] => 2 [4] => 4 [5] => 9 [6] => 1 )
    Copy after login
  • array_merge($arr1,$arr2),传进去两个数组,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。 如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。

  • unset($value) 传进去的变量可以销毁一个数组,也可以销毁数组中的一个值,当通过数组[下标]的方式传入是,会销毁数组中的这个值,但是当传进去的数组时,会把整个数组进行销毁。

  • sort() 本函数对数组进行排序。当本函数结束时数组单元将被从最低到最高重新安排。当数组中值都是数字的时候,可以使用这个函数进行排序,不过排序的结果是数字按照从小到大的顺序。

    <?php
    
        $arr = array(1,9,4,2,8,10,5);
    
        sort($arr);
    
        print_r($arr);
    .....结果.....
    Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 [4] => 8 [5] => 9 [6] => 10 )
    Copy after login
  • usort($array,func_name) 传进去一个数组,和一个自己写的方法名。使用用户自定义的比较函数对数组中的值进行排序,在上面的一个方法,你只能得到从小到大的顺序,但是我们的要求是从大到小,这就会用到这个函数,第二个参数是我们写的排序规则的函数。

    <?php
    
    $arr = array(1,9,4,2,8,10,5);
    
    function mysort($a,$b){ //写自己的函数,使数组按照从大到小的顺序进行排序。
    if($a == $b){
        return 0;
    }
    
        return $a < $b ? 1 : -1;
    }
    
    usort($arr, &#39;mysort&#39;);
    
    print_r($arr);
    ......结果......
    Array ( [0] => 10 [1] => 9 [2] => 8 [3] => 5 [4] => 4 [5] => 2 [6] => 1 )
    Copy after login

    这里使用到可变函数。

    数组的运算符

    我们在运算符那一节并没有讲到数组的运算符,其实数组也是有运算符的。

    • $a + $b 联合 $a 和 $b 的联合。运算符把右边的数组元素附加到左边的数组后面,两个数组中都有的键名,则只用左边数组中的,右边的被忽略。

    • $a == $b 相等 如果 $a 和 $b 具有相同的键/值对则为 TRUE 。

    • $a === $b 全等 如果 $a 和 $b 具有相同的键/值对并且顺序和类型都相同则为 TRUE 。

    • $a != $b 不等 如果 $a 不等于 $b 则为 TRUE 。

    • $a <> $b 不等 如果 $a 不等于 $b 则为 TRUE 。

    • $a !== $b 不全等 如果 $a 不全等于 $b 则为 TRUE 。  
      在这里看来数组的运算符也是很好理解的。

    总结

    数组讲到这里,算是基本讲完了,数组在我们的开发中是经常用到的,不止上面的以为数组,有时候数组里面还可能是一个数组,只要数组的结构在脑海中有清晰的认识,不管有几个数组,都是可以解决的,同时数组的出现也引出了一些关于数组的算法。

     以上就是PHP基础教程八之数组的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!