Hi @sr57
Maybe you could combine diff() and isEmpty() methods like this:
$col1->diff($col2)->isEmpty();
ref2: https://laravel.com/docs/8.x/collections#method-isempty
Hope this helps!
$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?
Hi @sr57
Maybe you could combine diff() and isEmpty() methods like this:
$col1->diff($col2)->isEmpty();
ref2: https://laravel.com/docs/8.x/collections#method-isempty
Hope this helps!
$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
}
Thanks @michaloravec
I already tested with == that does not work , I forgot === & != ... !
I have already tested, it does not work
if ( $col1->diff($col2)->isEmpty() ) echo "Ok"; else echo "KO";
// Ok
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!!
Thanks @marianomoreyra I fogot also to test with === and not with ==
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
@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
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)
@sr57 I don't know what you want to think about.
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!
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 ,
@sr57 I don't know why are you focusing on your first example. How I said you have to pass an array there..,
As I wrote before, in 'real life' I should have somewhere in my code a not 'well formed collection' and your solution is not obvious.
I like Laravel, but clearly I don't like such situation ... to be continued.
Maybe we can link with this post https://laracasts.com/discuss/channels/laravel/laravel-sanitary-check
@sr57 What does it mean not well formed collection?
And why my solution is not obvious?
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.
From the doc, collect returns a collection and I got no error with this syntax ... so it's a collection or I miss st
@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.
That's clear
Both examples you gave return collections, but the first one doesn't pass the test '==' , see previous post.
@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 No response?
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.