Skip to content

Instantly share code, notes, and snippets.

@vibby
Last active August 29, 2015 14:18
Show Gist options
  • Save vibby/3d0008bdf0f3f71eec06 to your computer and use it in GitHub Desktop.
Save vibby/3d0008bdf0f3f71eec06 to your computer and use it in GitHub Desktop.
<?php
function f($a,$k){
if(!is_array($a) || !array_key_exists($k, $a)) return false;
$s=$a[$k];
$b=$a;
$b[$k]=!$s;
$t=$a[$k]===!$s;
$a[$k]=$s;
return $t;
}
$keys = array('string', 152, 0, '');
$values = array('string', 152, 0, '', true, false, !null, null, (object)[], array(), array('k' => 'v'));
// testing non-array «a»
foreach ($keys as $key) {
foreach ($values as $value) {
var_dump(f($value, $key) === false);
}
}
// testing no-reference «a»
foreach ($keys as $key) {
foreach ($values as $value) {
var_dump(f(array($key => $value), $key) === false);
}
}
// testing referencing «a»
foreach ($keys as $key) {
foreach ($values as $value) {
$array = array();
$array[$key] =& $value;
var_dump(f($array, $key) === true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment