


PHP array merging, splitting, and differential value function set_PHP tutorial
There are three functions for merging arrays:
1.array_combine()
carries two parameter arrays. The value of parameter array one is used as the key of the new array, and the value of parameter array two is value as the value of the new array. Very simple.
Example:
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b) ;
print_r($c);
?>
The above example will output:
Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)
2.array_merge()
carries two parameter arrays, and simply appends array two to the back of array one Create a new array.
Example:
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => " trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
The above example will output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
3.array_merge_recursive()
is the same as the above function, the only difference is that When appending, it is found that the key to be added already exists. The processing method of array_merge() is to overwrite the previous key value. The processing method of array_merge_recursive() is to reconstruct the sub-array and form a new numerical array with the values of the repeated keys.
Example:
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green ", "blue"));
$result = array_merge_recursive($ar1, $ar2);
?>
The above example will output $result:
Array
(
[color] => Array
(
[ favorite] => Array
(
[0] => red
[1] => green
)
[0] => blue
)
[0] => 5
[1] => 10
)
There are two functions for splitting an array:
1.array_slice()
carries three parameters. Parameter one is the target array, parameter two is offset, and parameter three is length. The function is to extract a subarray of length starting from offset from the target array.
If offset is a positive number, the starting position is checked from the beginning of the array. If offset is a negative number, the starting position is checked from the end of the array. If length is a positive number, the number of subarray elements taken out is definitely length. If length is a negative number, the subarray starts from offset and ends at count (target array) - |length| from the beginning of the array. Specifically, if length is empty, the end position is at the end of the array.
Example:
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d" , and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a" , "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input , 2, -1, true));
?>
The above example will output:
Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)
2.array_splice()
carry The three parameters are the same as above, and their function is to delete the subarray with length starting from offset.
Example:
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
// "blue", "black", "maroon")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
// "blue", "purple", "yellow");
?>
区别取值函数有四个:
1.array_intersect()
携带参数不定,均为数组,返回所有数组中公共元素的值组成的数组,数组的键由所在第一个数组的键给出。
例子:
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
?>
上例将输出:
Array
(
[a] => green
[0] => red
)
2.array_intersect_assoc()
在前一个函数的基础上,返回所有数组中键、值均相同的键值对。
例子:
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result_array = array_intersect_assoc($array1, $array2);
?>
上例将输出:
Array
(
[a] => green
)
3.array_diff()
携带多个数组,返回第一个数组中有的而后面的数组中没有的所有的值组成的新数组,对应键取自第一个数组。
例子:
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
上例将输出:
Array
(
[1] => blue
)
4.array_diff_assoc()
在前一个函数的基础上,不仅需要匹配值还要匹配键。
例子:
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
?>
上例将输出:
Array
(
[b] => brown
[c] => blue
[0] => red
)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

The difference between Ethereum and Bitcoin is significant. Technically, Bitcoin uses PoW, and Ether has shifted from PoW to PoS. Trading speed is slow for Bitcoin and Ethereum is fast. In application scenarios, Bitcoin focuses on payment storage, while Ether supports smart contracts and DApps. In terms of issuance, the total amount of Bitcoin is 21 million, and there is no fixed total amount of Ether coins. Each security challenge is available. In terms of market value, Bitcoin ranks first, and the price fluctuations of both are large, but due to different characteristics, the price trend of Ethereum is unique.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The Ouyi OKEx digital asset trading platform is different from the traditional securities market. It is open for trading 24 hours a day, and users can conduct fiat currency trading, currency trading and contract trading at any time. However, the platform will announce in advance and temporarily adjust trading time or rules in case of system maintenance upgrades or special market events (such as extreme market conditions causing severe market fluctuations), such as suspending trading or modifying contract trading position opening rules. Therefore, it is recommended that users pay close attention to platform announcements and market trends, seize trading opportunities and do a good job in risk management. Only by understanding Ouyi OKEx trading time and rule adjustments can you be at ease in the digital currency market.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

The total amount of Bitcoin is constant at 21 million, and this fact set by Satoshi Nakamoto code gives Bitcoin a unique value attribute. Unlike unlimited fiat currencies, Bitcoin’s scarcity gives it anti-inflation potential and is similar to precious metals such as gold. Its issuance mechanism is gradually released through "mining" and the reward is halved every four years, and it is expected to reach the total upper limit by around 2140. Although the actual circulation volume is less than the total volume, the total volume limit of 21 million coins is the core value support of Bitcoin, and it also makes it a highly-watched digital asset. Understanding the limit on the total amount of Bitcoin is crucial for investors to make rational decisions.
