PHP notes 6-array and related operations


Array, a combination of data, refers to storing a group of data (multiple) in a specified container

Array definition syntax

In PHP, the system provides a variety of ways to define arrays
1. Use the array keyword: the most commonly used
$variable = array (element 1, element 2, element 3...);
2. You can use square brackets to wrap the data:
$variable = [Element 1, Element 2...];
3. Invisible definition array: add a bracket to the variable, the system automatically becomes an array
$variable[] = value 1; //If you don’t provide a subscript, the system automatically generates (number : Start from 0)
$variable[subscript] = value; //The content in the brackets is called the subscript key, and the subscript can be a letter

$a = array(1,'2',3,null,'dd');
var_dump($a);echo '<br/>';//array(5) { [0]=> int(1) [1]=> string(1) "2" [2]=> int(3) [3]=> NULL [4]=> string(2) "dd" } 
$b = [1,'2',3,null,'dd'];
var_dump($b);echo '<br/>';//array(5) { [0]=> int(1) [1]=> string(1) "2" [2]=> int(3) [3]=> NULL [4]=> string(2) "dd" }
$c[] = 123;
var_dump($c);echo '<br/>';//array(1) { [0]=> int(123) } 
$d[2] = 123;
var_dump($d);echo '<br/>';//array(1) { [2]=> int(123) } 

PHP array features

1) Integer subscripts or string subscripts can be used.
If the array subscripts are all integers: indexed array
If the array subscripts are all strings: associative array
2) Different subscripts can be mixed: mixed array
3) The order of array elements is The placing order shall prevail, and has nothing to do with the subscript.
4) The self-growth characteristic of the digital subscript: automatically grow from 0. If a larger one appears manually in the middle, then the subsequent self-growth elements start from the largest value +1
5) Special value Automatic conversion of subscripts
Boolean value: true and false
empty: NULL
6) There is no type restriction
on array elements in PHP 7) There is no length restriction on array elements in PHP

$a['age']=12;
echo $a['age'];//12
$a[0]='哈哈';
$a[1]='哈哈1';
$b[false]=false;
$b[true]=true;
$b[NULL]=NULL;
var_dump($a);//array(3) { ["age"]=> int(12) [0]=> string(4) "哈哈" [1]=> string(5) "哈哈1" } 
var_dump($b);//array(3) { [0]=> bool(false) [1]=> bool(true) [""]=> NULL }

Two-dimensional array

Two-dimensional array: the elements in the array are also arrays

$info = array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
print_r($info);
print_r($info[1][1]);//5

Multidimensional Arrays

The second-dimensional array element can continue to be an array, and there is no dimensional limit in PHP (PHP does not essentially have a two-dimensional array)

Alien array (irregular array)
Alien array: The elements in the array are irregular, and there are ordinary basic variables and arrays.
In actual development, it is not commonly used, try to make the array elements regular (easy to access)

Array traversal

1) Foreach traversal syntax

$arr=array(1,2,3,4,5,6,7,8);
foreach($arr as $v){
    
    
echo $v.'<BR/>';
};

foreach($arr as $k => $v){
    
    
echo $k.'='.$v.'<BR/>';
}

$info = array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
foreach($info as $v){
    
    
	echo $v[0].' '.$v[1].' '.$v[2];
	}

2) For loop to traverse the array

a r r = a r r a y ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ) ; f o r ( arr=array(1,2,3,4,5,6,7,8); for( arr=array(1,2,3,4,5,6,7,8);for(i = 0, l e n = c o u n t ( len = count( l e n=count(arr); i < i< i<len;KaTeX parse error: Expected '}', got 'EOF' at end of input: …echo ' key is'.i.’ value is ‘. a r r [ arr[ arr[i].’
’;
}

Array related functions

1. Sorting function Sort
array elements are compared according to ASCII code, and can be compared in English.
sort(): order sorting (subscript rearrangement, the relationship between subscript and value changes)
rsort(): reverse order sorting (same as above )
Asort(): order sorting (subscripts are retained, and the corresponding relationship between subscripts and values ​​remains unchanged)
arsort(): reverse order (same as above)
ksort(): order sort: according to the key name (subscript)
krsort(): reverse order Sorting: According to the key name (subscript)
shuffle(): Randomly shuffle the array elements, and the array subscripts will be rearranged

$arr=array(1,2,3,4,5,6,7,8);
$a = shuffle($arr);
var_dump($arr);//array(8) { [0]=> int(4) [1]=> int(6) [2]=> int(3) [3]=> int(8) [4]=> int(1) [5]=> int(7) [6]=> int(2) [7]=> int(5) }
sort($arr);
var_dump($arr);//array(8) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) }

2. Pointer function
reset(): reset the pointer, point the array pointer to the first position
end(): reset the pointer, point the array pointer to the last element
next(): move the pointer down, get the value of the next element
prev(): Move the pointer up to get the value of the previous element
current(): get the element value corresponding to the current pointer
key(): get the subscript value
next and prev corresponding to the current pointer will move the pointer, which may cause the pointer to move to the front or the end ( Leaving the array), the array cannot be used, and the correct pointer position cannot be returned through next and prev. The pointer can only be reset by end or reset.
3. Other functions
count(): count the number of elements in the array
array_push(): add an element to the array (the end of the array)
array_pop(): pop an element from the array and return (The end of the array)
array_shift(): pop an element from the array and return (the beginning of the array)
array_unshift(): add an element from the array (the beginning of the array)

array_reverse(): return an array with the reversed element order
in_array(): determine whether an element exists in the array
array_keys(): get all the subscripts of an array, return an index array
array_values(): get all the values ​​of an array, return An index array

$a = [];
array_push($a,3);
array_push($a,2);
array_push($a,1);
array_unshift($a,4);
var_dump($a);//array(4) { [0]=> int(4) [1]=> int(3) [2]=> int(2) [3]=> int(1) } 
echo '<br/>';
echo array_pop($a).'<br>';//1
echo array_shift($a).'<br>';//4
var_dump($a);//array(2) { [0]=> int(3) [1]=> int(2) }
echo '<br/>';

var_dump(in_array(3,$a));//bool(true) 
echo '<br/>';
var_dump(array_values($a));//array(2) { [0]=> int(3) [1]=> int(2) } 
echo '<br/>';

Guess you like

Origin blog.csdn.net/zhangxm_qz/article/details/108538825