-2
Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
            [0] => User Object
                (
                    [original:protected] => Array
                        (
                            [user_id] => 123456
                            [active] => 1
                            [name] => ABC XYZ
                            [first_name] => ABC
                            [last_name] => XYZ
                            [email] => [email protected]
                            [username] => abcxyz
                            [secret_code] => S4#$sdD                           
                        )
                )

            [1] => User Object
                (

                    [original:protected] => Array
                        (
                            [user_id] => 987654
                            [active] => 1
                            [name] => CBD IHK
                            [first_name] => CBD
                            [last_name] => IHK
                            [email] => [email protected]
                            [username] => seCdils
                            [secret_code] => S4#$sdD
                        )

                )

        )

)

Identify both object array has same secret_code return boolean if exist true else false tried array_count_values return only string and number please guide thanks

I just want to check if detect secret_code same in array give me true else false

3
  • 4
    Possible duplicate of PHP unique array by value? Commented May 29, 2018 at 10:04
  • Not a duplicate Commented May 29, 2018 at 10:42
  • I just want to check if detect secret_code same in array give me true else false Commented May 29, 2018 at 11:01

1 Answer 1

1
<?php

$dupes = []; // keep track of duplicates
foreach ($users as $user1) { // iterate over all items
    $dupeCount = 0; // because we iterate over the same array, we always find at least the item itself (1 dupe minimum)

    foreach ($users as $user2) { // check the array again
        if ($user1 === $user2) { // if they are exactly the same: http://php.net/manual/en/language.oop5.object-comparison.php
            $dupeCount++;
        }
        if ($dupeCount > 1) { // because we always find at least 1, push only when we find more than that
            array_push($dupes, $user1); // add it to the result
        }
    }
}

However for you comment

I just want to check if detect secret_code same in array give me true else false

which should be in your question to begin with.

collect($users)->unique(function ($item) {
    return $item['secret_code'];
});
Sign up to request clarification or add additional context in comments.

4 Comments

What is collect..? says undefined
collect is a function that creates a collection from an array: laravel.com/docs/5.6/collections
Bdw I'm using 4.2 this does not work, hope you understand my question if $users[0]['secret_code'] exactly match with $users[1]['secret_code'] should return true else false
@Ross you using v4.2 is something you should mention before, this way you do too little effort and the person(s) trying to answer you have to do too much guesswork.

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.