Home  >  Article  >  Backend Development  >  A brief discussion on PHP source code 10: About array_keys, array_values ​​functions

A brief discussion on PHP source code 10: About array_keys, array_values ​​functions

不言
不言Original
2018-06-28 17:03:012179browse

This article mainly introduces a brief discussion on PHP source code 10: Regarding the array_keys and array_values ​​functions, it has a certain reference value. Now I share it with you. Friends in need can refer to it

The first of the new year This article, first of all, I would like to wish all my friends a Happy New Year

I have some time today to read the code and write something as a souvenir!

array array_keys (array input [, mixed search_value [, bool strict]] )

array_keys() Returns the key name of the number or string in the input array.

If the optional parameter search_value is specified, only the key name of the value will be returned. Otherwise all keys in the input array will be returned. Since PHP 5, you can use the strict parameter to perform equality comparisons (===).

The array_keys function is implemented in line 2416 of the standard/array.c file PHP_FUNCTION(array_keys)

The program follows the usual PHP style and first determines whether the input is correct. If there is a third parameter, Then the function to determine the size uses is_identical_function (the is_equal_function function is used by default)
Then initialize the returned array, traverse the given array, take the key value of each element, and assign it to the returned array. This key value is divided into There are two types of numbers and strings. The most important function is the hash operation function zend_hash_get_current_key_ex (get the key value of the current element)

  ZEND_API int zend_hash_get_current_key_ex(HashTable *ht, char **str_index, uint *str_length, ulong *num_index, zend_bool duplicate, HashPosition *pos){
    Bucket *p; 
    p = pos ? (*pos) : ht->pInternalPointer; 
    IS_CONSISTENT(ht);     if (p) {
        if (p->nKeyLength) {    //    数字型的nKeyLength长度为0
            if (duplicate) {
                *str_index = estrndup(p->arKey, p->nKeyLength - 1);
            } else {
                *str_index = p->arKey;    //    /* arKey存储key名称(字符串类型的key)必须是最后一个成员,*/
            }
            if (str_length) {
                *str_length = p->nKeyLength;
            }
            return HASH_KEY_IS_STRING;
        } else {
            *num_index = p->h;    //    存储数字key值
            return HASH_KEY_IS_LONG;
        }
    }
    return HASH_KEY_NON_EXISTANT;}

The understanding of this function mainly depends on the understanding of the bucket definition

For bucket-related content, please go to http://www.php.cn/php-weizijiaocheng-405316.html

array array_values ​​(array input)

array_values() Returns the input array All values ​​in and create numerical indexes for them.
The array_values ​​function is basically similar to the array_keys function implementation, and it also lacks a zend_hash_get_current_key_ex operation and an operation to determine the value type.

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

Related recommendations:

A brief discussion of PHP source code nine: Introduction to array_unshift, array_push

A brief discussion of PHP source code eight : Introduction to array_pop, array_shift

A brief discussion of PHP source code 7: About nl2br, ltrim, rtrim, trim functions

The above is the detailed content of A brief discussion on PHP source code 10: About array_keys, array_values ​​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