我有一个PHP数组如下:

$messages = [312, 401, 1599, 3, ...];

我想删除包含值$del_val的元素(例如,$del_val=401),但我不知道它的键。这可能会有帮助:每个值只能在那里出现一次。

我正在寻找执行这项任务的最简单的函数。


当前回答

使用array_search()和unset,尝试以下操作:

if (($key = array_search($del_val, $messages)) !== false) {
    unset($messages[$key]);
}

Array_search()返回它找到的元素的键,可以使用unset()从原始数组中删除该元素。它将在失败时返回FALSE,但在成功时返回FALSE -y值(例如,您的键可能为0),这就是使用严格比较操作符!==的原因。

if()语句将检查array_search()是否返回值,如果返回值,则只执行操作。

其他回答

借用了underscore.JS _的逻辑。拒绝并创建了两个函数(人们更喜欢函数!!)

array_reject_value:该函数简单地拒绝指定的值(也适用于PHP4,5,7)

function array_reject_value(array &$arrayToFilter, $deleteValue) {
    $filteredArray = array();

    foreach ($arrayToFilter as $key => $value) {
        if ($value !== $deleteValue) {
            $filteredArray[] = $value;
        }
    }

    return $filteredArray;
}

array_reject:这个函数只是拒绝可调用方法(适用于PHP >=5.3)

function array_reject(array &$arrayToFilter, callable $rejectCallback) {

    $filteredArray = array();

    foreach ($arrayToFilter as $key => $value) {
        if (!$rejectCallback($value, $key)) {
            $filteredArray[] = $value;
        }
    }

    return $filteredArray;
}

所以在我们当前的例子中,我们可以像下面这样使用上述函数:

$messages = [312, 401, 1599, 3, 6];
$messages = array_reject_value($messages, 401);

或者甚至更好:(因为这给了我们更好的语法使用,比如array_filter)

$messages = [312, 401, 1599, 3, 6];
$messages = array_reject($messages, function ($value) {
    return $value === 401;
});

上面的代码可以用于更复杂的事情,比如我们想要删除所有大于或等于401的值,我们可以简单地这样做:

$messages = [312, 401, 1599, 3, 6];
$greaterOrEqualThan = 401;
$messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
    return $value >= $greaterOrEqualThan;
});
function array_remove_by_value($array, $value)
{
    return array_values(array_diff($array, array($value)));
}

$array = array(312, 401, 1599, 3);

$newarray = array_remove_by_value($array, 401);

print_r($newarray);

输出

Array ([0] => 312 [1] => 1599 [2] => 3)

我认为最简单的方法是使用带有foreach循环的函数:

//This functions deletes the elements of an array $original that are equivalent to the value $del_val
//The function works by reference, which means that the actual array used as parameter will be modified.

function delete_value(&$original, $del_val)
{
    //make a copy of the original, to avoid problems of modifying an array that is being currently iterated through
    $copy = $original;
    foreach ($original as $key => $value)
    {
        //for each value evaluate if it is equivalent to the one to be deleted, and if it is capture its key name.
        if($del_val === $value) $del_key[] = $key;
    };
    //If there was a value found, delete all its instances
    if($del_key !== null)
    {
        foreach ($del_key as $dk_i)
        {
            unset($original[$dk_i]);
        };
        //optional reordering of the keys. WARNING: only use it with arrays with numeric indexes!
        /*
        $copy = $original;
        $original = array();
        foreach ($copy as $value) {
            $original[] = $value;
        };
        */
        //the value was found and deleted
        return true;
    };
    //The value was not found, nothing was deleted
    return false;
};

$original = array(0,1,2,3,4,5,6,7,4);
$del_val = 4;
var_dump($original);
delete_value($original, $del_val);
var_dump($original);

输出将是:

array(9) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
  [3]=>
  int(3)
  [4]=>
  int(4)
  [5]=>
  int(5)
  [6]=>
  int(6)
  [7]=>
  int(7)
  [8]=>
  int(4)
}
array(7) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
  [3]=>
  int(3)
  [5]=>
  int(5)
  [6]=>
  int(6)
  [7]=>
  int(7)
}

一个有趣的方法是使用array_keys():

foreach (array_keys($messages, 401, true) as $key) {
    unset($messages[$key]);
}

array_keys()函数接受两个额外的参数,只返回特定值的键,以及是否需要严格检查(即使用===进行比较)。

这也可以删除具有相同值的多个数组项(例如[1,2,3,3,4])。

或者简单地说,手动方式:

foreach ($array as $key => $value){
    if ($value == $target_value) {
        unset($array[$key]);
    }
}

这是最安全的,因为你可以完全控制你的阵列