search
HomeBackend DevelopmentPHP TutorialA brief discussion of PHP source code 14: About the array_combine function

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;  }

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();

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!

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
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.