Home > Backend Development > PHP Tutorial > A brief discussion of PHP source code 14: About the array_combine function

A brief discussion of PHP source code 14: About the array_combine function

不言
Release: 2023-04-01 22:10:02
Original
1652 people have browsed it

This article mainly introduces the fourteenth discussion about PHP source code: Regarding the array_combine function, it has certain reference value. Now I share it with you. Friends in need can refer to it.

The tenth discussion about PHP source code Four: About the array_combine function

array_combine

(PHP 5)
array_combine — Create an array, use the value of one array as its key name, and the value of another array as its value
Description

array array_combine (array keys, array values)

Returns an array, using the value from the keys array as the key name, and the value from the values ​​array as the corresponding value.
Return FALSE if the number of cells in the two arrays is different or the array is empty.

Program implementation instructions:

 array_init(return_value); 
 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(keys), &pos_keys);
 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(values), &pos_values);    //    初始化数组指针,将其置为双向链接的头指针
 while (zend_hash_get_current_data_ex(Z_ARRVAL_P(keys), (void **)&entry_keys, &pos_keys) == SUCCESS &&
   zend_hash_get_current_data_ex(Z_ARRVAL_P(values), (void **)&entry_values, &pos_values) == SUCCESS) {    //    同时遍历两个数组
  if (Z_TYPE_PP(entry_keys) == IS_STRING) {    //    如果key值为字符串,以key
   zval_add_ref(entry_values);
   add_assoc_zval_ex(return_value, Z_STRVAL_PP(entry_keys), Z_STRLEN_PP(entry_keys)+1, *entry_values);
  } else if (Z_TYPE_PP(entry_keys) == IS_LONG) {
   zval_add_ref(entry_values);
   add_index_zval(return_value, Z_LVAL_PP(entry_keys), *entry_values);
  } else {
   zval key;
   key = **entry_keys;
   zval_copy_ctor(&key);
   convert_to_string(&key);    //    转化为字符串,如果为数组,则为Array
   zval_add_ref(entry_values);
   add_assoc_zval_ex(return_value, Z_STRVAL(key), Z_STRLEN(key)+1, *entry_values);
   zval_dtor(&key);
  }
  zend_hash_move_forward_ex(Z_ARRVAL_P(keys), &pos_keys);
  zend_hash_move_forward_ex(Z_ARRVAL_P(values), &pos_values);    //    下一个元素,其实现为:pos_values = pos_values->pListNext;  }
Copy after login

In the PHP code, if the key array contains two arrays, the latter one will overwrite the previous one, that is, there will be only one element in the end,

is as follows Shown PHP code:

  \<?PHP$arr1 = array(1, array(1, 2), array(3, 4), array(5, 6));$arr2 = array(33, 44, 55, 66);$arr3 = array_combine($arr1, $arr2);print_r($arr3);die();
Copy after login

This code will output:

Array ( [1] => 33 [Array] => 66 )

EOF

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 Thirteen: Introduction to array_change_key_case, array_chunk

A brief discussion on PHP source code Twelve: About return_value Return value

Brief discussion on PHP source code Eleven: About array_key_exists, introduction to in_array

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template