This article mainly introduces about the PHP source code 20: About the Array_flip,array_reverse function, has a certain reference value, now share to everyone, the need for friends can refer to
Talking about PHP source code 20: About Array_flip,array_reverse function
Array_flip
(PHP 4, PHP 5)
Array_flip-exchanging keys and values in an array
Description
Array Array_flip (array trans)
Array_flip () returns a reversed array, such as the key name in trans becomes a value, and the value in trans is the key name.
Note that the value in trans needs to be able to be a valid key name, such as an integer or string. If the value is of the wrong type, a warning will be issued and the problematic key/value pair will not be reversed.
If the same value occurs more than once, the last key will be its value, and all the others are lost.
Array_flip () If the failure returns FALSE.
SOURCE Description: Iterate over the hash table where the array is located, take out the key, create a new zval, and then write the value of the previous array as key, to the hash table of the returned array,
For the previous value, only long and string are supported, and if there are other types then warning, the source of the type is as follows:
if (z_type_pp (entry) = = Is_long) {zend_hash_index_update (z_arrval_p (Return_value), z_lval_pp (entry), &data, sizeof (data), NULL); } else if (z_type_pp (entry) = = is_string) {zend_symtable_update (z_arrval_p (Return_value), z_strval_pp (entry), Z_ STRLEN_PP (Entry) + 1, &data, sizeof (data), NULL);} else {zval_ptr_dtor (&data);/* would free also zval structure */php_error_docref (NULL tsrmls_cc, e_warning, "Can only F Lip STRING and INTEGER values! ");}
Array_reverse
(PHP 4, PHP 5)
array_reverse-returns an array of cells in reverse order
Description
Array array_reverse (array array [, BOOL Preserve_keys])
Array_reverse () takes an array of arrays as input and returns a new array with a cell in reverse order, preserving the original key name if Preserve_keys is TRUE.
SOURCE Description: Since the array is stored in the hash table, while the hash table supports a doubly linked list,
So the program starts with the last element of the doubly linked list and traverses forward, copying all the elements in the process (ie, (*entry)->refcount++;)
and writes it back to the hash table where the array is located.
On the hash table in the two-way list can see the definition of hash table, in the previous article has introduced: http://www.php.cn/php-weizijiaocheng-405316.html
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!