PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 6, 2022

[FIXED] get array_intersect count without duplicates php

 January 06, 2022     codeigniter, php     No comments   

Issue

I have 2 arrays:

$first = array(1,1,2,3,4);
$second = array(1,2,3,4,5);

when i use $count = array_intersect($first, $second);, and count($count); the matches, it shows 5, resulting in 1 intersecting twice. I need to get 4, which doesn't count duplicates.

how can I achieve that in php?

thanks in advance.


Solution

It's very simple: first make sure your arrays are unique, and then apply array_intersect:

$count = count(array_intersect(array_unique($first), array_unique($second)));

Repro case on 3v4l.



Answered By - vixducis
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing