Sort out several methods commonly used in php to process arrays, so that you can learn more. you can run them one by one and get familiar with the use of php arrays. array_shift () removes the first unit of the array and returns the result. This is convenient for the first unit of the array to be processed independently.
The code is as follows:
$ Tmparray = array ("1", "2", "3", "4 ");
$ Tmparray = array_shift ($ tmparray );
Print_r ($ tmparray );
?>
2. array_chunk () splits an array into multiple arrays, and the following parameters control the number of array units.
The code is as follows:
$ Tmparray = array ('A', 'B', 'C', 'D', 'E ');
Print_r (array_chunk ($ tmparray, 2 ));
?>
This function is useful in some loops. for example, I want to put the obtained data into N rows and M columns. If we directly loop, we certainly won't be able to implement it. of course, nested loops can be implemented, but it is too troublesome. if we use the array_chunk () function to process and then return a new array, it is very convenient.
3. array_push () pushes the input value to the end of the array.
The code is as follows:
$ Tmparray = array ("a", "B ");
Array_push ($ tmparray, "c", "d ");
Print_r ($ tmparray); // Array ([0] => a [1] => B [2] => c [3] => d)
?>
4. array_unshift () inserts the incoming unit into the array.
The code is as follows:
$ Tmparray = array ("a", "B ");
$ Resarray = array_unshift ($ tmparray, "c", "d ");
Print_r ($ resarray) // Array ([0] => a [1] => B [2] => c [3] => d)
?>
5. array_unique de-duplicates the array and returns a new array.
The code is as follows:
$ Tmparray = ("a" => "a", "B" => "B", "c" => "c ", "d" => "B ");
$ Resarray = array_unique ($ tmparray); // ("a" => "a", "B" => "B", "c" => "c ");
?>