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>';
                    }
                }
            }
?>
2
  • 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. Commented Feb 27, 2017 at 6:07
  • see this post:stackoverflow.com/questions/17231925/… Commented Feb 27, 2017 at 6:11

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@15ongm Can you give a link of your code snippet. Try inputting your data in the ideone link that I have provided.
0

You can use array_filter with count:

$is_in_array = count(array_filter($gymnasts, function($member) use ($gymnast) {
    return $member == $gymnast;
})) > 0;

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.