Home Backend Development PHP Tutorial PHP basic array merging, splitting, and distinguishing value function sets_PHP tutorial

PHP basic array merging, splitting, and distinguishing value function sets_PHP tutorial

Jul 13, 2016 pm 05:45 PM
php the difference value merge Base Split array of

PHP array merging, splitting, and differential value function set
It is said that the array function of PHP is very powerful and can only be felt when it is actually used in project work. At least I think so. Now I have slowly discovered the mystery...
There are three functions for merging arrays:
1.array_combine()
It 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 used 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()
Carrying two parameter arrays, simply append array two to array one to form a new array.
Example:
$arrayarray1 = array("color" => "red", 2, 4);
$arrayarray2 = 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()
It 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 replace the repeated keys. The values ​​form a new numeric array.
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 arrays:
1.array_slice()
It 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 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()
It carries three parameters, the same as above, and its 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()
携带参数不定,均为数组,返回所有数组中公共元素的值组成的数组,数组的键由所在第一个数组的键给出。
例子:
$arrayarray1 = array("a" => "green", "red", "blue");  
$arrayarray2 = 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  
)   

作者“飞翔的人生”

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478628.htmlTechArticleThe merging, splitting, and distinguishing function sets of PHP arrays say that PHP's array functions are very powerful, and only true You can only feel it when you use it in project work, at least I think so, now it has slowly...
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles