Delete the first element in the array (red) and return the element that was deleted:
<?php $a =array ("a" and "Red", "b" = "green", "c" = "blue"); echo array_shift ($a);p rint_r ($a);? >
Definition and usage
The Array_shift () function deletes the first element in the array and returns the element that was deleted.
Note: If the key name is numeric, all elements will get a new key name, starting with 0 and incrementing at 1 (see example below).
Grammar
Array_shift (Array)
Parameter description
Array required. Specifies the array.
Technical details return value:
Returns the value of the element removed from the array, or null if the array is empty.
Use numeric Key name:
<?php$a=array (0=> "Red",1=> "green",2=> "Blue"); Echo array_shift ($a);p rint_r ($a);? >
A large PHP array (1w+), using Array_shfit and array_pop to take array elements, the performance gap is particularly large, array_shift slow unbearable, and Array_pop quickly.
Don't say the answer first, look at the paragraph code:
$arr = Array ( 0=>123, 3=>132, 2=>987,) array_shift ($arr);//array_pop ($arr); Var_dump ($arr); What is the difference between the output, after Array_shift, the output is: Array (2) { [0]=> int ( [1]=>) int (987)}array_pop, the output is: Array ( 2) { [0]=> int (123) [3]=> Int (132)}
What's the difference?
Yes, that is, after the array_shift operation, the key value of the array changes. This is the reason why Array_shift is slow. Because the Array_shift operation array will re-start rebuilding from 0 to the numeric key value. Each time you move an element, you have to iterate through all the elements in the array. Array_pop would not have been. One is O (1), one is the complexity of O (n), and after the array is large, the effect is obvious.
The C code implementation of the corresponding function in PHP:
/* {{{void _phpi_pop (internal_function_parameters, int off_the_end) */static void _phpi_pop (Internal_function_ PARAMETERS, int off_the_end) {zval *stack,/* Input stack */**val;/* Value to be popped */char *key = Null;uint Key_len = 0 ; ULong Index;if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "a", &stack) = = FAILURE) {return;} if (Zend_hash_num_elements (z_arrval_p (stack)) = = 0) {return;} /* Get The first or last value and copy it into the return value */if (off_the_end) {zend_hash_internal_pointer_end (Z_ARRV Al_p (Stack));} else {zend_hash_internal_pointer_reset (z_arrval_p (stack));} Zend_hash_get_current_data (z_arrval_p (stack), (void * *) &val); Retval_zval (*val, 1, 0);/* Delete the first or last value */ZEND_HASH_GET_CURRENT_KEY_EX (z_arrval_p (Stack), &key, &am P;key_len, &index, 0, NULL); if (Key && z_arrval_p (stack) = = &eg (symbol_table)) {Zend_delete_global_ Variable (key, key_len-1 tsrmls_cc);} else {Zend_hash_del_key_or_index (z_arrval_p (stack), key, Key_len,Index, (key)? Hash_del_key:hash_del_index);} Here it is, traversing all the elements and re-assigning the elements that are numeric keys. The off_the_end of pop is 1,shift off_the_end is 0/* If we did a shift ... re-index like it did before */if (!off_the_end) {unsigned int K = 0;int Should_rehash = 0; Bucket *p = z_arrval_p (Stack)->plisthead;while (P! = NULL) {if (p->nkeylength = = 0) {//Key value is a number if (p->h! = k) {P-> ; h = K++;should_rehash = 1;} else {k++;}} p = p->plistnext;} Z_arrval_p (stack)->nnextfreeelement = k;if (Should_rehash) {//Because the key is assigned a value, after the hash position may be different, you have to re-hash, put it in the corresponding position. Zend_hash_rehash (z_arrval_p (stack));}} else if (!key_len && index >= z_arrval_p (stack)->nnextfreeelement-1) {z_arrval_p (stack), Nnextfreeelement = z_arrval_p (Stack)->nnextfreeelement-1;} Zend_hash_internal_pointer_reset (z_arrval_p (stack));}
php_function (array_pop) {_phpi_pop (internal_function_param_passthru, 1);} Php_function (array_shift) {_phpi_pop (internal_function_param_passthru, 0);}