7 (Introduction to PHP array)

 

catalogue

1, Array basis

2, Definition of array

3, Assignment of array

4, Value of array

5, Classification of arrays

6, Some common system functions of arrays

1, Array basis

In php, array is an ordered collection of several data. It has the following characteristics: ① there are no special requirements for the data type of the data stored in the array; ② The order of array elements has nothing to do with subscripts, but is determined by the order in which the data is put; ③ The subscripts of an array can be integers and strings.

2, Definition of array

3, Assignment of array

4, Value of array

1. The value of the array can be obtained by adding subscript to the variable name of the array:

2. Array traversal value. Array traversal refers to taking out the subscript and data of the array in turn:

$arr = array(3,'bb',14,2.1,'a'=>5);

foreach ($arr  as  $key => $value){

    //Traversal statement;

}

notes:

$arr , is an array variable

As can be seen as dividing an array into subscripts and data

$key - subscript

$value - data value

 

 

5, Classification of arrays

1. Key value relationship score:

① Index array:

The subscript of the array is the subscript of a continuous integer starting from 0, - which is the same as the js array

For example:

 $arr1 = array(2,3,5,76,8);

② Associative array:

The subscript of an array is a string written to indicate the meaning of the array data;

 $emp= array('Id'=>1,'Name'=>'Xiao Ming','age'=>22,'salary'=>1000.4);

2. By complexity:

① One dimensional array

Each data contained in the array is a non array value

For example:

 $arr2 = array(2,3,5,76,8);

② 2D array:

The array contains a one-dimensional array

For example:

$arr3 = array(

              array(1,2,3),

              array(4,5,6),

              array(7,8,9),

              10,
              11,
              33
       );

③ 3D array or multidimensional array:

An array is embedded with an array of three or more layers

 

6, Some common system functions of arrays

array_pop(): delete the last item of the array

array_push(): put a new cell at the last position of the array

sort(): sort the values of the array in positive order

asort(): arrange the values of the array in positive order and maintain the key value relationship

in_array(): judge whether a data exists in an array

count(): count to get the length of the array

range(): set the limit of two data and return the array corresponding to all data values in this range

array_merge(): merge arrays

<?php
$arr1 = array(1,2,3,9,4,6);
$arr2 = array(1,'a',"b",$arr1,false);

//array_pop(): delete the last item of the array
var_dump($arr1);//array(6) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(9) [4]=> int(4) [5]=> int(6) } 
array_pop($arr1);
var_dump($arr1);//array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(9) [4]=> int(4) } 

//array_push(): put a new cell at the last position of the array
array_push($arr1,'xinzeng');
var_dump($arr1);//array(6) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(9) [4]=> int(4) [5]=> string(7) "xinzeng" } 

//sort(): sort the values of the array in positive order
sort($arr1);
var_dump($arr1);//array(6) { [0]=> string(7) "xinzeng" [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) [5]=> int(9) } 

//asort(): arrange the values of the array in positive order and maintain the key value relationship
var_dump($arr2);//array(5) { [0]=> int(1) [1]=> string(1) "a" [2]=> string(1) "b" [3]=> array(6) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(9) [4]=> int(4) [5]=> int(6) } [4]=> bool(false) } 
asort($arr2);
var_dump($arr2);//array(5) { [4]=> bool(false) [1]=> string(1) "a" [2]=> string(1) "b" [0]=> int(1) [3]=> array(6) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(9) [4]=> int(4) [5]=> int(6) } } 

echo '<br>'.'--------------------------';
//in_array(): judge whether a data exists in an array
echo in_array(1,$arr2);//1
echo in_array('ff',$arr2);//null

//count(): count to get the length of the array
echo count($arr2);//5

//range(): set the bounds of 2 data and return the array corresponding to all data values in this range, such as
//If 1,5 is set, an array with values from 1 to 5 will be returned
var_dump(range(1,5));//array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } 

//array_merge(): merge arrays
$arr01 = array(1,2);
$arr02 = array(1,3);
var_dump(array_merge($arr01,$arr02));//array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(1) [3]=> int(3) } 
?>

Keywords: PHP

Added by gelwa on Sun, 16 Jan 2022 10:06:39 +0200