sr57's avatar
Level 39

Collections equality test

$col1=collect('a','a','2','3');
$col2=collect('a','2','3','3');

What should be the smarter code to test than $col1 not equal to $col2?

0 likes
23 replies
MichalOravec's avatar
Level 75
$col1 = collect(['a','a','2','3']);

$col2 = collect(['a','a','2','3']);

$col1 == $col2; // true

$col1 = collect(['a','a','2','3']);

$col2 = collect(['a','2','3','3']);

$col1 == $col2; // false

So just

if ($col1 != $col2) {
    // they are different
}
sr57's avatar
Level 39

Thanks @michaloravec

I already tested with == that does not work , I forgot === & != ... !

sr57's avatar
Level 39

Hi @marianomoreyra

I have already tested, it does not work

if ( $col1->diff($col2)->isEmpty() ) echo "Ok"; else echo "KO";

// Ok
MarianoMoreyra's avatar

You're right @sr57 , in this case that the elements are repeated, it doesn't work!

Anyway, I forgot that Collections are easier to compare, just like @michaloravec pointed out!!

sr57's avatar
Level 39

@michaloravec @marianomoreyra

Bets answer still to be found.

Sorry but we concluded too quicky.

$col1=collect('a','a','2','3');
$col2=collect('a','a','2','3');
echo "Ok expected : ";
if ( $col1 === $col2 ) echo "Ok"; else echo "KO";
if ( $col1 !== $col2 ) echo "KO"; else echo "Ok";
if ( $col1 != $col2 ) echo "KO"; else echo "Ok";
if ( $col1 == $col2 ) echo "Ok"; else echo "KO";

// Ok expected : KOKOOkOk


$col1=collect('a','a','2','3');
$col2=collect('a','2','3','3');
echo "<br>KO expected : ";
if ( $col1 === $col2 ) echo "Ok"; else echo "KO";
if ( $col1 !== $col2 ) echo "KO"; else echo "Ok";
if ( $col1 != $col2 ) echo "KO"; else echo "Ok";
if ( $col1 == $col2 ) echo "Ok"; else echo "KO";

// KO expected : KOKOOkOk

MichalOravec's avatar

@sr57 === and !== don't work. You have to use only == or !=.

This create collection which contain only letter a.

collect('a','a','2','3');

You need to use square brackets.

collect(['a','a','2','3']);

By the way I gave you a correct answer.

A collection is instance of Illuminate\Support\Collection so it's a classic class and here is an explaination why you can't use a strict compare operators.

https://www.php.net/manual/en/language.oop5.object-comparison.php

1 like
sr57's avatar
Level 39

@michaloravec

You have to use only == or !=

does not work in the second example*

need to use square brackets

In 'real life' collections come from calculations and are more much more complex than this simple example.

*It's late, hope I'am not wrong ... to be continued tomorrow (for me)

MarianoMoreyra's avatar

@sr57

In 'real life' collections come from calculations and are more much more complex than this simple example

True, but also they come well formed as opposed to your example, as @michaloravec explained.

If you were to compare 2 collections, returned from a calculation or even a query from DB, using == or != should work.

Try that tomorrow with a fresh mind so you can be sure that @michaloravec reply is the best answer.

Have a nice sleep!

sr57's avatar
Level 39

Good morning @michaloravec and @marianomoreyra

After a good night, I confirm that both of you are right ... but I'm not wrong

The collect method returns a new Collection instance

See below, my first example is not Ok, of course it should not be a "well formed" collection, but as written before, it's a Collection instance, I miss probably st.

function aff($test,$exp,$res) {
	echo "$test";
	if ( $exp == $res ) echo " Ok , "; else echo " KO , ";
}

function test($exp,$col1,$col2) {
	# exp : 1 col1=col2, 0 col1!=col2
	echo "<br>test : $col1 $col2 : ";
#	if ( $col1 === $col2 ) $res=1; else $res=0;aff("===",$exp,$res);
#	if ( $col1 !== $col2 ) $res=0; else $res=1;aff("!==",$exp,$res);
	if ( $col1 != $col2 ) $res=0; else $res=1;aff("!=",$exp,$res);
	if ( $col1 == $col2 ) $res=1; else $res=0;aff("==",$exp,$res);
}

$col1=collect('a','a','2','3');
$col2=collect('a','a','2','3');
test(1,$col1,$col2);
$col2=collect('a','2','3','3');
test(0,$col1,$col2);
echo "<br>";

// test : ["a"] ["a"] : != Ok , == Ok ,
// test : ["a"] ["a"] : != KO , == KO ,

$col1=collect(['a','a','2','3']);
$col2=collect(['a','a','2','3']);
test(1,$col1,$col2);
$col2=collect(['a','2','3','3']);
test(0,$col1,$col2);
echo "<br>";

// test : ["a","a","2","3"] ["a","a","2","3"] : != Ok , == Ok ,
// test : ["a","a","2","3"] ["a","2","3","3"] : != Ok , == Ok , 
MichalOravec's avatar

@sr57 I don't know why are you focusing on your first example. How I said you have to pass an array there..,

MichalOravec's avatar

@sr57 What does it mean not well formed collection?

And why my solution is not obvious?

sr57's avatar
Level 39

Collections that seem equal but don' pass the test == like the ones in the previous example.

Since I haven't find the "pb" in my code I have no other example.

sr57's avatar
Level 39

From the doc, collect returns a collection and I got no error with this syntax ... so it's a collection or I miss st

MichalOravec's avatar

@sr57 collect() accept only one parameter.

collect('a', 'a', '2', '3')->toArray(); // ['a']

So you need to pass it as an array

collect(['a', 'a', '2', '3'])->toArray(); // ['a', 'a', '2', '3']

When you told that the first example didn't work, it doesn't matter because you don't create a collection.

sr57's avatar
Level 39

@michaloravec

That's clear

Both examples you gave return collections, but the first one doesn't pass the test '==' , see previous post.

MichalOravec's avatar

@sr57 What else can I say....

$col1 = collect('a', 'a', '2', '3');

$col2 = collect('a', '2', '3', '3');

$col1 == $col2 // true

And it's correct because

$col1 = collect('a', 'a', '2', '3'); // this is same as collect(['a']);

$col2 = collect('a', '2', '3', '3'); // this is same as collect(['a']);

So what I said is correct.

sr57's avatar
Level 39

@michaloravec

What else can I say....

Many things :-)

  • that I'm wrong from the beginning

  • that communication is a difficult art

  • that communication is always a question of repetition

  • that I really understand slowly

  • ...

many THANKS for your time.

Please or to participate in this conversation.