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

时间:2022-08-26 14:15:23

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.

当然,这种情况是没有道理的。我的目标是在将$control与$data进行比较之前,在我的条件的最后一部分从$control中删除一个键。

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?

如果是,如何?

4 个解决方案

#1


4  

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.

副作用是$control不会被条件修改。

#2


1  

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():

但这并不能真正回答你的问题……如果您希望它在一行中,您可能需要检查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.

它的可读性不强,可能也不是很有效。

#3


0  

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;
    }
?>

#4


0  

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;
}

#1


4  

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.

副作用是$control不会被条件修改。

#2


1  

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():

但这并不能真正回答你的问题……如果您希望它在一行中,您可能需要检查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.

它的可读性不强,可能也不是很有效。

#3


0  

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;
    }
?>

#4


0  

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;
}