Yii Framework Forum: in_array() vs. array_key_exists() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

in_array() vs. array_key_exists() Rate Topic: -----

#1 User is offline   Tropi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 30-November 11

Posted 05 April 2012 - 03:24 PM

Hi, everyone.
Can someone explain why PHP behaves the way it does, in this example?
I have this little demo script:
<?php
$arr = array('apple', 'orange', 'banana');
var_dump(in_array('no i dont have this here!!!', array_keys($arr))); // return true
var_dump(array_key_exists('no i dont have this here!!!', $arr)); // returns false
?>


Now my question is: Why does in_array() returns true in this case? I thought that these constructs would return the same (false).

Thanks for your help.
0

#2 User is offline   Nanobyte 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 11-March 12
  • Location:Germany, Lower Saxony

Posted 05 April 2012 - 05:00 PM

Quote

Searches haystack for needle using loose comparison unless strict is set.


Set the last parameter of in_array (strict) to TRUE and the result is correct ;)
0

#3 User is offline   Tropi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 30-November 11

Posted 05 April 2012 - 05:26 PM

Yes, you are right, but I still don't understand why. This parameter makes sense if I want to test for 1 but not for "1". In my case the string "no i dont..." obviously doesn't exist in any type in this array.

btw. This is also mentioned on the manual page: http://at.php.net/ma...rray.php#106319
However there isn't an explanation either.
0

#4 User is offline   Nanobyte 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 11-March 12
  • Location:Germany, Lower Saxony

Posted 06 April 2012 - 07:23 AM

(Sorry my English is not the best)


I think you dont want to use array_keys.
You want array_values right?

var_dump(in_array('no i dont have this here!!!', array_values($arr))); // return false




If you NOT enable strict-mode then PHP converts your string to int(0) and the first key of your array is int(0)

If strict-mode is enabled, PHP checks the type also.

<?php
$arr = array('apple', 'orange', 'banana');

var_dump(in_array('no i dont have this here!!!', array_keys($arr))); // return true

var_dump(in_array('no i dont have this here!!!', array_keys($arr)), true ); // return false

var_dump(array_key_exists('no i dont have this here!!!', $arr)); // returns false


var_dump( 'no i dont have this here!!!' ); // string(27)


var_dump( (int)'no i dont have this here!!!' ); // int(0)


var_dump( (array_keys($arr)) ) /* Output:
array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
}
*/

1

#5 User is offline   Tropi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 30-November 11

Posted 06 April 2012 - 12:04 PM

Okay, I think I understoof now. in_array() looks something like this.
function in_array($needle, $haystack) {
  foreach ($haystack as $element) {
    if ($needle == $element) 
      return true;
  }
  
  return false;
}


With the values above called in the second iteration there is always "somestring" == 1, which is true, since every non-empty string == 1. However in strict mode the comparison would be "somestring" === 1 which is obviously not true.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users