How to remove duplicate elements from two arrays in php

藏色散人
Release: 2023-03-12 21:32:01
Original
1949 people have browsed it

php method to remove duplicate elements from two arrays: 1. Create a PHP sample file; 2. Use the foreach statement and the unset function to delete the same elements in the two arrays.

How to remove duplicate elements from two arrays in php

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

How to remove duplicate elements from two arrays in php?

PHP deletes the same elements in two arrays:

The code is as follows:

<?php
	$invite1 = &#39;18,19,20,21,22&#39;;
	$invite2 = &#39;18,19&#39;;
	$a = explode(",",$invite1);
	$b = explode(",",$invite2);
	
	foreach ($a as $key=>$v1) {
		foreach($b as $key2=>$v2){
			if($v1==$v2){
			unset($a[$key]);//删除$a数组同值元素
			unset($b[$key2]);//删除$b数组同值元素
			}
		}
	}
	var_dump($a);	
	var_dump($b);
?>
Copy after login

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to remove duplicate elements from two arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!