0

I have an array that stores some values. I'm trying to detect the similar values and add them to new array.

example:

  $arrayA = array( 1,4,5,6,4,2,1);

  $newarray = (4,1);

Any help?

3
  • By "similar" do you mean "duplicated"? Commented Sep 22, 2013 at 18:02
  • what are you saying EXACTLY ??? Commented Sep 22, 2013 at 18:23
  • i'm trying to create two arrays from one. In array A i want separate duplicated values and insert them into array B and array A includes only single values Commented Sep 22, 2013 at 18:40

4 Answers 4

1

Use the array_intersect() method. For example

$arrayA = array(1,4,5,6,4,2,1);
$arrayB = array(4,1);

$common_values = array_intersect($arrayA, $arrayB);
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

$array = array(1,4,5,6,4,2,1);
$duplicates = array_unique(array_diff_assoc($array, array_unique($array)));

Comments

0
$a1 = array( 1,4,5,6,4,2,1);
$a = array();
foreach($a1 as $value){
  if(!in_array($value, $a)){
    $a[] = $value;
  }
}

Comments

0
$arrayA = array(1,4,5,6,4,2,1);
$newarray = array_diff_assoc($arrayA, array_unique($arrayA));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.