11

I've got an array with multiple person-objects in it, this objects look like this:

id: 1,
name: 'Max Muster',
email: '[email protected]',
language: 'German'

Now, I've got objects in another array, which doesn't look exactly the same:

id: 1,
name: 'Max Muster',
email: '[email protected]',
language: 'de'

I've got a foreach-loop to loop through array 2 and check if the objects exists in array 1.

foreach($array2 as $entry) {
    if(existsInArray($entry, $array1)) {
        // exists
    } else {
        // doesn't exist
    }
}

Is there a function to check (like my existsInArray()), if my object exists in the array? I just need to check, if the object-id exists, other attributes doesn't matter.

5
  • use array_diff to check the both array is same or not. Commented May 30, 2016 at 11:50
  • 1
    As the objects are different, then you can't do a simple comparison to see if they are the same. What qualifies an object from the first array as matching an object from the second array? Same id? Same name? Same email? Same language? Some combination of those? Commented May 30, 2016 at 11:52
  • Yes, need to convert them Object to array than array_diff. Commented May 30, 2016 at 11:52
  • 1
    You could make this a lot easier if you used the id's as the array keys. Commented May 30, 2016 at 11:54
  • It's just the id, which has to be the same Commented May 30, 2016 at 13:13

4 Answers 4

19

Use the object IDs as keys when you put the objects in the array:

$array1[$object->id] = $object;

then use isset($array1[$object->id]) to check if the object already exists in $array:

if (isset($array1[$object->id])) {
    // object exists in array; do something
} else {
    // object does not exist in array; do something else
}
Sign up to request clarification or add additional context in comments.

2 Comments

this looks like it checks by index, but isn't index arbitrary? How does this work?
PHP Arrays are, in fact, maps/hashes/dictionaries. They associate a numeric or string key to a value of any type. The OP has two sets of objects identified by their id attributes. The answer suggests to put the first set of objects in an array and use the id as key. When the second set of objects is processed, use the object id to find the object with the same id in the array. By using the id as key (and not a numeric key generated automatically), the object can be identified without scanning the entire array.
5

It does not, but you can write it:

function existsInArray($entry, $array) {
    foreach ($array as $compare) {
        if ($compare->id == $entry->id) {
            return true;
        }
    return false;
}

Comments

0
foreach($array2 as $entry) {
    if(in_array($entry, $array1)) {
        // exists
    }  else {
    // doesn't exist
    }
}

Use in_array to check if that particular object exists in the array

Comments

0

Use in_array

if(in_array($object, array)) {
   //exists
} else {
   //does not exist
}

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.