0

If the $gymnast object is not in the $gymnasts array, I add it to my table. However, when I add the same object to the array, in_array() fails (returns 1) and the duplicate object is added to the array. Using print_r, I saw that the $gymnast object is clearly the same as the first element in the $gymnasts array, so why is this happening? How do I fix this?

$gymnasts array

Array ( [0] => Gymnast Object ( [name] => Nastia Liukin [age] => 27 [height] => 5' 3" [olympicYear] => 2008 [medalCount] => 5 [image] => nastia.jpg ) [1] => Gymnast Object ( [name] => Shawn Johnson [age] => 25 [height] => 4' 11" [olympicYear] => 2008 [medalCount] => 4 [image] => shawn.jpg ))

$gymnast object

Gymnast Object ( [name] => Nastia Liukin [age] => 27 [height] => 5' 3" [olympicYear] => 2008 [medalCount] => 5 [image] => nastia.jpg )

index.php

<?php
function isDuplicateEntry($gymnast, $gymnasts) {
    foreach ($gymnasts as $gym) {
        $gymArr = get_object_vars($gym);
        $gymnastArr = get_object_vars($gymnast);
    if (count(array_diff($gymnastArr, $gymArr)) == 0) { //gymnast object already exists in array
        return true;
    }
    else {
        return false;
    }
    }
}

 //Add gymnast when press add submit button
        if(isset($_POST['add'])){
            //Set gymnast array to all gymnasts in data.txt
            $gymnasts = read_file($filename);

            //If form has valid elements & no duplicats, add to data.txt file
            if($valid_name && $valid_age && $valid_feet && $valid_inches && $valid_olympicYear && $valid_medalCount && $valid_image){
                $gymnast = get_gymnast_from_form();

                //Make sure gymnast is not null
                if(!is_null($gymnast)){
                    //Prevent from adding duplicates
                    if(!isDuplicateEntry($gymnast, $gymnasts)){
                        //Write task to file
                        write_file($filename, $gymnast);
                        //Add gymnast to global var gymnasts array
                        $gymnasts[] = $gymnast;
                        echo'<div class ="title">Gymnast Added!</div>';
                    }
                }
            }
?>
Frits
  • 7,341
  • 10
  • 42
  • 60
14wml
  • 4,048
  • 11
  • 49
  • 97
  • Hi. The in_array function is probably comparing the objects using there references and not its keys and values. Try implementing a function to replace in_array. – Lucas Piske Feb 27 '17 at 06:07
  • see this post:http://stackoverflow.com/questions/17231925/php-in-array-object-comparing – Suchit kumar Feb 27 '17 at 06:11

2 Answers2

0

You cannot use in_array for multidimensional objects straight away. You will have to loop them to find out the match. Use array_intersect to check if the array is present in another array.

$flag = 0;
foreach ($gymnasts as $gym {
    if (count(array_intersect($gymnast, $gym)) == count($gym)) {
        echo 'It is present.';
        $flag = 1;
        break;
    }
}

if ($flag == 0) {
    echo 'Not present in array';
}

You can also use array_diff to match arrays.

$flag = 0;
foreach ($gymnasts as $gym) {
    if (count(array_diff($gymnast, $gym)) == 0) {
        echo 'It is present.';
        $flag = 1;
        break;
    }
}

if ($flag == 0) {

    echo 'Not present';
}

array_intersect

array_diff

ideone link array_intersect

ideone link array_diff

Ayush
  • 741
  • 9
  • 19
  • I tried this (see updated code above) but it didn't work, even though `$gymArr$` and `$gymnastArr$` are both `Array ( [name] => Nastia Liukin [age] => 27 [height] => 5' 3" [olympicYear] => 2008 [medalCount] => 5 [image] => nastia.jpg ) Array ( [name] => Nastia Liukin [age] => 27 [height] => 5' 3" [olympicYear] => 2008 [medalCount] => 5 [image] => nastia.jpg )`. Am I dong something wrong? – 14wml Feb 27 '17 at 07:09
  • @15ongm Can you give a link of your code snippet. Try inputting your data in the ideone link that I have provided. – Ayush Feb 27 '17 at 07:13
0

You can use array_filter with count:

$is_in_array = count(array_filter($gymnasts, function($member) use ($gymnast) {
    return $member == $gymnast;
})) > 0;
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78