Page 1 of 1

in_array/array_search always find everything

Posted: Wed Jan 19, 2011 10:15 am
by Lars282
Hey,

I am trying to write a log in that requires the user to be a member of a secret facebook group. Hence, I am asking user for permission to access his groups list and then fetch it and try to search for the specific group ID. For some reason in_array and array_search always find whatever I am searching for. You can try here: http://quadro-design.org/uj/index.php (I promise to delete you from the list of people who gave access to their profiles afterwards!)

The relevant code for what is done is this,

Code: Select all

$ch = curl_init('https://graph.facebook.com/me/groups?access_token=' . $fb_cookie['access_token']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$user_groups = json_decode(curl_exec($ch),true);
curl_close($ch);

foreach ($user_groups['data'] as $key => $value) {
	var_dump($value);
	var_dump(array_search("LALALA", $value));
	echo '<br />';
and the relevant result is this.

Code: Select all

array(3) { ["version"]=> int(0) ["name"]=> string(19) "Facebook Developers" ["id"]=> string(10) "2205007948" } string(7) "version" 
array(4) { ["version"]=> int(1) ["name"]=> string(13) "UJ Test Group" ["id"]=> string(15) "145134142210424" ["administrator"]=> bool(true) } string(13) "administrator" 
array(4) { ["version"]=> int(0) ["name"]=> string(5) "ICMUN" ["id"]=> string(10) "2210610727" ["administrator"]=> bool(true) } string(7) "version" 
array(3) { ["version"]=> int(0) ["name"]=> string(21) "OxIMUN 2010 - SPECPOL" ["id"]=> string(15) "106393712753441" } string(7) "version" 
array(3) { ["version"]=> int(0) ["name"]=> string(54) "OxIMUN 2010- Oxford International Model United Nations" ["id"]=> string(15) "113685065319968" } string(7) "version" 
array(3) { ["version"]=> int(0) ["name"]=> string(20) "Thinking Development" ["id"]=> string(15) "129667607051176" } string(7) "version" 
array(3) { ["version"]=> int(0) ["name"]=> string(27) "Imperial College Yacht Club" ["id"]=> string(10) "2204176629" } string(7) "version" 
[...]
How come that "version" => 0 or "administrator" => true are found when searching for LALALA??

Thanks so much!

Lars

Re: in_array/array_search always find everything

Posted: Wed Jan 19, 2011 10:27 am
by Darhazer
the comparision "lalala" == true is true :)
use the $strict mode of array_search:

Code: Select all

var_dump(array_search("LALALA", $value, true));

Re: in_array/array_search always find everything

Posted: Wed Jan 19, 2011 10:47 am
by Lars282
Could you explain where the comparison LALALA == true is made?

Also, trying $strict mode with the amended code seems to work:

Code: Select all

define('FACEBOOK_GROUP', '145134142210424');

$ch = curl_init('https://graph.facebook.com/me/groups?access_token=' . $fb_cookie['access_token']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$user_groups = json_decode(curl_exec($ch),true);
curl_close($ch);

foreach ($user_groups['data'] as $key => $value) {
        var_dump($value);
        var_dump(array_search(FACEBOOK_GROUP, $value, true));
        echo '<br />';

Code: Select all

array(3) { ["version"]=> int(0) ["name"]=> string(19) "Facebook Developers" ["id"]=> string(10) "2205007948" } bool(false) 
array(4) { ["version"]=> int(1) ["name"]=> string(13) "UJ Test Group" ["id"]=> string(15) "145134142210424" ["administrator"]=> bool(true) } bool(false) 
array(4) { ["version"]=> int(0) ["name"]=> string(5) "ICMUN" ["id"]=> string(10) "2210610727" ["administrator"]=> bool(true) } bool(false) 
Could you explain how the $strict mode made the difference? Why does it need the $strict mode for this?

Thanks!

Re: in_array/array_search always find everything

Posted: Wed Jan 19, 2011 11:09 am
by Jonah Bron
Strict mode tells the function to compare with '===', instead of '=='. '==' compares their value, but '===' compares their type and value. Look at the following example:

Code: Select all

echo 123 == '123'; // returns true
echo 123 === '123'; // returns false
They have the same value, but their types are different. One is an integer, and one is a string. Here are some more true statements:

Code: Select all

1 == true
2 == true
0 == false
'' == false
'hello' == true
null = false
All of those statements are true with '==', but would be false with '===' because their types are different. Any null, zero, or empty value (including an empty array) evaluates to false. Anything else evaluates to true.

See the manual for more info.

http://php.net/operators.comparison

Re: in_array/array_search always find everything

Posted: Wed Jan 19, 2011 11:14 am
by Lars282
Thanks you two!!