There is a dedicated data structure solution in a strong-type language. Generally, a container is created to store any type of data in the container, and the container capacity can be determined based on the data stored in the container to achieve a variable-length container structure, for example, linked lists, stacks, and queues are common forms in data structures. In PHP, arrays are usually used to complete the work that can be completed only when data structures are used in other languages. It is a weak type language. It can store multiple types of data in the same array, and the array in PHP has no length limit, the size of the data stored in the array can also be adjusted automatically based on the increase or decrease of the number of elements.
I. Stack using Arrays
Stack is an implementation form of data structure and a container that is widely used to store data. In a container such as a stack, the final pushed data (into the stack) will be first popped up (out of the stack ). That is, the data structure is "advanced and later" in data storage. In PHP, the array is used as a stack. The system functions array_push () and array_pop () can be used to complete data import and export operations.
① Function array_push ()
The array_push () function adds one or more elements (in the stack) to the end of the array of the first parameter, and returns the length of the new array. This function is equivalent to calling $ array [] = $ value multiple times. The function prototype is as follows:
Int array_push (array & array, mixed var [, mixed...])
The first parameter of the function is required and serves as an array of the stack container. The second parameter is also required, and a data is added at the end of the array in the first parameter. You can also add multiple optional parameters to the end of the array of the first parameter, that is, the inbound stack. Note that even if the array contains a string key name, the added element is always a numeric key. The code used by the array_push () function is as follows:
1234567891011121314 |
<?php $a = array ( "Dog" , "Cat" ); array_push ( $a , "Horse" , "Bird" ); print_r( $a ); // Output array ([0] => dog [1] => CAT [2] => horse [3] => bird) // Array with string keys: $a = array ( "a" => "Dog" , "b" => "Cat" ); array_push ( $a , "Horse" , "Bird" ); print_r( $a ); // Output array ([a] => dog [B] => CAT [0] => horse [1] => bird) // Use the array_push () function and use this direct value assignment to initialize an array. $lamp [ "web" ]= "www" ; print_r( $a ) // Output array ([a] => dog [B] => CAT [0] => horse [1] => BIRD [web] => www) ?> |
If array_push () is used to add a unit to the array, it is better to use the "$ array [] = $ value" format, because there is no additional burden to call the function, the latter can also be used to add an associated array where the key value is a string. If the first parameter is not an array, array_push () generates a warning. This is different from the action of "$ array [] = $ value". The latter creates an array.
② Array_pop (array & array)
The array_pop function deletes the last element in the array, pops up the last element of the array (the output stack), and removes the length of the array by 1. If the array is empty (or not an array) returns null. The function prototype is as follows:
Mixed array_pop (array & array)
This function has only one parameter, that is, the stack data. Returns the value of the last element in the pop-up array. The code used by the array_pop () function is as follows:
1234567 |
<?php // Declare an array as a stack $lamp = array ( "Linux" , "Apache" , "MySQL" , "PHP" ); array_pop ( $lamp ); print_r( $lamp ) // Output array ([0] => Linux [1] => Apache [2] => MySQL) ?> |
Ii. Queue using Arrays
The array processing function in PHP can also use arrays to perform queue operations. A stack is based on the "back-to-first-out" principle, while a queue allows data to be inserted at one end and deleted at the other end. This means that the data first enters the queue is first exited from the queue, just like the bank's number generator, the first scheduled number is the first to handle the business. That is, the queue is the principle of "first-in-first-out.
Use the array_push () and array_pop () functions to add and delete data from the end of the array. If you use the array_push () function to add data at the end of the array, the first element in the array can be deleted to implement a queue.
③ Function array_shift ()
The array_shift () function deletes the first element in the array and returns the value of the deleted element. The function prototype is as follows:
Mixed array_shift (array & array)
Like the array_pop () function, this function has only one required parameter, which is an array for implementing queues. Removes the first unit from the array and returns the result. The length of the array is reduced by 1 and all other elements are moved forward by one. All numeric keys are counted from 0, and the string key names remain unchanged. If the array is null (or not an array), null is returned. The code used by the array_shift () function is as follows:
12345678910 |
<?php // Associated array with string key value $lamp = array ( "a" => "Linux" , "b" => "Apache" , "c" => "MySQL" , "d" => "PHP" ); array_shift ( $lamp ); print_r( $lamp ); // Output: array ([B] => Apache [c] => MySQL [d] => PHP) $lamp = array ( "Linux" , "Apache" , "MySQL" , "PHP" ); array_shift ( $lamp ); print_r( $lamp ); // Re-index array with numeric subscript ([0] => Apache [1] => MySQL [2] => PHP) ?> |
④ Function array_unshift ()
In PHP, you can use the array_unshift () function to insert one or more elements at the beginning of the queue array. If the function is successfully executed, the number of inserted elements is returned. The format and function array_push () are used () is the same. The preceding four functions are used to add and delete data from any end of the array.
> Fixed link: http://php.ncong.com/php_course/arry_function/shujujiegou.html
> For Reprinted Information, please note: ncong PHP was published in ncong PHP learning tutorial on September 10, April 14, 2014.