Define true or false if in array two equal elements are present

Elena Politi

Hi excuse my stupid question, but I am having troubles in finding the way to return true or false in a function that go through an array like the following one

Array
(
    [0] => Array
        (
            [0] => 1389
        ),

    [1] => Array
        (
            [0] => 1389
        ),

    [2] => Array
        (
            [0] => 14568
        ),

    [3] => Array
        (
            [0] => 14568
        )

)

I should be able to return true or false if in the array two equal elements are present or not. For example in the example array I should be able to return true, as 1389 and 14568 are both present.

On the opposite, if the array is made like this:

Array
(
    [0] => Array
        (
            [0] => 1389
        ),

    [1] => Array
        (
            [0] => 1389
        ),
)

the output should then be false

I tried to set the cycle as this, but obviously it doesn't work!

$products_id_array []= array( $product_id); // my array
$count = count($products_id_array);

$products = $products_id_array[0][0];//set the first term of teh array
$i=0;
while ($i<$count) {
  if ( $products_ids[$i][0] == $products)
  {
      $products = $products_ids[$i][0];
      $mixed_products = false;
  }
  else
  {
      $mixed_products = true;
  }
  $i++;
}
return $mixed_products;

What am I doing wrong? Thanks!

BehradKhodayar

You can do it using array_diff & array_intersect functions:

$arr1 = [[1389], [1389], [14568], [14568]];
$arr2 = [[1389], [1389]];
$arr3 = [3, 3, 4, 4, 5 ];

function checkDoubleDuplicate(array $argArray) {
  $iflag = 0;
  $arrayUnique = array_unique($argArray, SORT_REGULAR);
  foreach ($arrayUnique as $key => $value) {
    $occuranceCount = count(array_intersect($argArray, array($value)));
    if ($occuranceCount == 2)
      $iflag++;
  }
  if (2 == $iflag)
    return TRUE;
  else
    return FALSE;
}

echo checkDoubleDuplicate($arr3) ? 'yes' : 'no';

The only thing you'd better do is to convert your complicated/redundant array(using array for every single value, thus making memory/code complicated) to a simple form like $arr3. I've suggested solution with this ToDo in mind

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why does !!1=="1" equal true and !!2=="2" equal false?

From Dev

Is the preprocessor macro "#define TRUE FALSE'" valid?

From Dev

How it is possible in Java for false to equal true

From Dev

Setting elements of an array equal to zero

From Dev

Check if all elements in an array are equal

From Dev

Checking if two elements are equal in JavaScript

From Dev

How to Perform True False Comparison on Two different array values

From Dev

JQuery or JavaScript - Count how many array elements are true/false

From Dev

Create a array of random True/False

From Dev

Expect two elements to be equal

From Dev

Remove array elements that are present in another array

From Dev

JAVA two equal strings return false

From Dev

#define TRUE !FALSE vs #define TRUE 1

From Dev

Checking to see if array elements are equal

From Dev

Pandas any() returning false with true values present

From Dev

Write a function that takes two lists, and returns True if the first list is the reverse of the same elements in the second list, and False otherwise

From Dev

Comparing two equal vars return false

From Dev

Expect two elements to be equal

From Dev

What is the best way to select array elements using an array of true/false values?

From Dev

Compare two array and return an array to show which elements are equal or not

From Dev

Only show array elements if it is present in another array

From Dev

Determining if all elements in array A are present in array B

From Dev

Why does True == True evaluate to True, when {a statement that's equal to True} == True evaluates to false?

From Dev

Why does np.array_equal return False for these two sparse arrays that are visually equal?

From Dev

check if all the elements in the array are equal

From Dev

Javascript function to return true or false, if specific characters are present in an array

From Dev

Coypu wait until one of the two elements is present

From Dev

Arrays with elements less or equal to the elements in given array

From Dev

Relate indices where two dataframes are equal with elements in another array

Related Related

  1. 1

    Why does !!1=="1" equal true and !!2=="2" equal false?

  2. 2

    Is the preprocessor macro "#define TRUE FALSE'" valid?

  3. 3

    How it is possible in Java for false to equal true

  4. 4

    Setting elements of an array equal to zero

  5. 5

    Check if all elements in an array are equal

  6. 6

    Checking if two elements are equal in JavaScript

  7. 7

    How to Perform True False Comparison on Two different array values

  8. 8

    JQuery or JavaScript - Count how many array elements are true/false

  9. 9

    Create a array of random True/False

  10. 10

    Expect two elements to be equal

  11. 11

    Remove array elements that are present in another array

  12. 12

    JAVA two equal strings return false

  13. 13

    #define TRUE !FALSE vs #define TRUE 1

  14. 14

    Checking to see if array elements are equal

  15. 15

    Pandas any() returning false with true values present

  16. 16

    Write a function that takes two lists, and returns True if the first list is the reverse of the same elements in the second list, and False otherwise

  17. 17

    Comparing two equal vars return false

  18. 18

    Expect two elements to be equal

  19. 19

    What is the best way to select array elements using an array of true/false values?

  20. 20

    Compare two array and return an array to show which elements are equal or not

  21. 21

    Only show array elements if it is present in another array

  22. 22

    Determining if all elements in array A are present in array B

  23. 23

    Why does True == True evaluate to True, when {a statement that's equal to True} == True evaluates to false?

  24. 24

    Why does np.array_equal return False for these two sparse arrays that are visually equal?

  25. 25

    check if all the elements in the array are equal

  26. 26

    Javascript function to return true or false, if specific characters are present in an array

  27. 27

    Coypu wait until one of the two elements is present

  28. 28

    Arrays with elements less or equal to the elements in given array

  29. 29

    Relate indices where two dataframes are equal with elements in another array

HotTag

Archive