Page 1 of 1
Array in comparison syntax... possible?
Posted: Wed Nov 18, 2009 4:06 pm
by batfastad
Hi everyone
Just had a thought... is there a way to compare the result of a function where the result is an array by adding the key number to the end of the function?
Some sort of syntax like this?
Code: Select all
if ($var == explode(',', $string)[1] ) {
The only alternative I can think of is setting the result of explode() to a variable and then doing the compare.
Is there an alternative syntax/way of doing things I'm missing out on?
Cheers, B
Re: Array in comparison syntax... possible?
Posted: Wed Nov 18, 2009 4:23 pm
by requinix
Nope. Not yet, at least. And no easy alternative that I can think of.
Posted: Wed Nov 18, 2009 4:31 pm
by Jonah Bron
Bummer, isn't it? You can do it in several other languages

Re: Array in comparison syntax... possible?
Posted: Wed Nov 18, 2009 5:00 pm
by Weiry
tasairis wrote:Nope. Not yet, at least. And no easy alternative that I can think of.
If you really had to compare against the exact key of the array, you could just use this.
Code: Select all
if (array_search($var,explode(',', $string)) == 1) { // where 1 is the key your checking against
print "ok";
}else{
print "not ok";
}
Re: Array in comparison syntax... possible?
Posted: Thu Nov 19, 2009 1:25 am
by batfastad
Yeah fair enough.
As it goes I'll need to do comparison on a couple of other members of the array so it makes sense to explode it to a variable first.
Just thought that adding an array key to the end of a function would be a useful bit of syntax if it existed.
Cheers for the help

Re: Array in comparison syntax... possible?
Posted: Thu Nov 19, 2009 3:25 am
by jackpf
I agree. I was thinking this a while ago. Javascript does it!!!