Home  >  Article  >  Backend Development  >  A brief discussion on PHP source code 20: About array_flip, array_reverse functions

A brief discussion on PHP source code 20: About array_flip, array_reverse functions

不言
不言Original
2018-06-28 17:44:341902browse

This article mainly introduces about the PHP source code 20: Regarding the array_flip and array_reverse functions, it has certain reference value. Now I share it with you. Friends in need can refer to it.

A brief talk about PHP Source code twenty: About array_flip, array_reverse function

array_flip
(PHP 4, PHP 5)

array_flip — Exchange keys and values ​​​​in arrays
Description
array array_flip (array trans)

array_flip() Returns a reversed array, for example, the key names in trans become values, and the values ​​in trans become key names.

Note that the value in trans needs to be a legal key name, for example, it needs to be integer or string. A warning will be emitted if the value is of the wrong type, and the key/value pair in question will not be reversed.

If the same value appears multiple times, the last key name will be used as its value, and all others will be lost.

array_flip() returns FALSE if it fails.
Source code description: Traverse the Hash Table where the array is located, take out the key, create a new ZVAL, and then use the value of the previous array as the key to write it into the hash table of the returned array.
Only the previous value is supported LONG and STRING, if there are other types, a warning will be issued. The source code for type judgment 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); /* will free also zval structure */php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can only flip STRING and INTEGER values!");}

array_reverse

(PHP 4, PHP 5)

array_reverse — Returns a unit Array in reverse order
Description
array array_reverse (array array [, bool preserve_keys])

array_reverse() accepts array array as input and returns a new array with cells in reverse order, if preserve_keys is TRUE retains the original key name.
Source code description: Since the array is stored in the Hash Table, and the Hash Table supports doubly linked lists,
So the program starts from the last element of the doubly linked list, traverses forward, and in the process, all elements are Make a copy (i.e., (*entry)->refcount ;)
and write this to the hash table where the returned array is located.

Regarding the doubly linked list in the hash table, you can view the definition of hash table, which was introduced in the previous article: http://www.php.cn/php-weizijiaocheng-405316.html

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

A brief discussion on PHP source code 19: About array_file, range function

A brief discussion on PHP source code 10 Eight: About array_diff_key, array_diff_assoc, array_udiff_assoc function

The above is the detailed content of A brief discussion on PHP source code 20: About array_flip, array_reverse functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn