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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),