Home >Backend Development >PHP Problem >How to merge different array elements in php

How to merge different array elements in php

藏色散人
藏色散人Original
2021-11-08 09:38:092423browse

php method to merge different array elements: 1. Create a PHP sample file; 2. Use "array_keys(array_flip($a) array_flip($b) array_flip($c));" to multiple Just merge the arrays to remove duplicates.

How to merge different array elements in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How does php merge different array elements?

Multiple array deduplication

array_keys(array_flip($arr1)+array_flip($arr2))

array_keys() function returns a new array containing all the key names in the array.

If the second parameter is provided, only the key name with the key value is returned.

Return a new array containing all the key names in the array:

<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a));
?>

Running results:

Array ( [a] => red [b] => green )
Array ( [0] => Volvo [1] => BMW [2] => Toyota )

Techniques for merging and deduplicating multiple arrays

$a = array(&#39;1001&#39;,&#39;1002&#39;);
$b = array(&#39;1002&#39;,&#39;1003&#39;,&#39;1004&#39;);
$c = array(&#39;1003&#39;,&#39;1004&#39;,&#39;1005&#39;);
$d = array_keys(array_flip($a) + array_flip($b) + array_flip($c));

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to merge different array elements in php. 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