从条件中删除关联数组中的特定键

I ran into a coding situation where i'd prefer to keep a certain condition as compact as possible:

// $data and $control are arrays
if($data==$control || ($someBool && $data==$control))
    return $c;

Of course this condition makes no sense that way. My goal is to remove a key from $control in the last part of my condition, before comparing it against $data.

Of course it could be done like this:

function chopByKey(array $arr, $key){
    if(!isset($arr[$key]))
        return $arr;
    unset($arr[$key]);
    return $arr;
}

And rewrite the condition:

if($data==$control || ($someBool && $data==chopByKey($control, 'someKey') ))
    return $c;

Please note

I am looking for a solution that i can use within my condition, not any solution that requires any additional step ahead of the condition or the definition of a custom function, be it anonymous or not.

My question is

Is there any more elegant way to do this, without defining a new custom function?

If yes, how?

I came up with the following line:

$control = array('hello' => 'world', 'foo' => 'bar');
$data = array('hello' => 'world');
$someBool = true;

if ($data == $control || $someBool && $data == array_diff_key($control, array('foo' => 0))) {

Side effect is that $control is not modified by the condition.

What I'd do is:

$checkControl = $control;
if ($someBool) unset($checkControl['somekey']);

if ($checkControl == $data) {

It requires 2 extra lines, but the whole is very readable.

But it doesn't really answer your question... If you want it in 1 line, you might want to check array_diff_assoc():

$diff = array_diff_assoc($control, $data);
if (!$diff || ($someBool && array_keys($diff) == array('somekey'))) {

It's not very readable and probably not very efficient.

Something like this may look elegant:

<?php
    $data = array(
    "one" => "1",
    "two" => "2"
    );
    $control  = array(
    "one" => "1",
    "two" => "2",
    "three" => "3"
    );
    if ($data==$control || ($someBool && $data==array_slice($control,0,count($control)-1))
    {
        return $c;
    }
?>

As for your function, I'd have done it like this:

function chopByKey(array $arr, $key){
  if(array_key_exists($key,$arr)){
    unset($arr[$key]);
    return $arr;
  }
  // depending on your desired result, either return $arr or return false here.
  return $arr;
}