简体   繁体   English

删除数组的特定值

[英]deleting specific value of array

I have an array containing the numbers 1 to 10. When i select one random array key, i want to delete this one The following code does something like it, but just not good enough. 我有一个包含数字1到10的数组。当我选择一个随机数组键时,我想删除此键。下面的代码做了类似的事情,但还不够好。

$imgArray = range(1,9);
$rand_key = array_rand($imgArray);
$imgValue = $imgArray[$rand_key];
unset($imgArray[$imgValue]);

The code deletes a value from the array, but it deletes the wrong one, Echoing the array gives the following result: 该代码从数组中删除了一个值,但删除了一个错误的值,回显该数组将得到以下结果:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)

Random selected item would be like number 4 but then it deletes array KEY 4, not array value 4.. 随机选择的项将类似于数字4,但是它将删除数组KEY 4,而不是数组值4。

Is there any way to change that? 有什么办法可以改变吗? besides changing the variable(using -1)? 除了更改变量(使用-1)?

As such there is no way to delete the unset the value by function but the You pay the trick for simplest function? 因此,无法通过函数删除未设置的值,但是您将花钱买最简单的函数吗? Well, you won't find anything simpler. 好吧,您将找不到更简单的东西。

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

$array=array_diff($array, array($toDelete));

array_search will return the key for the value that you search for, if it finds it: 如果找到, array_search将返回您要搜索的值的键:

$rand_key = array_rand($imgArray);
$imgKey   = array_search($imgArray[$rand_key]);

if ($imgKey !== false) {
    unset($imgArray[$imgKey]);
}

Of course you don't really need to do this, because $rand_key is already a key in the array ( array_rand returns a key !) so the code reduces to: 当然,您实际上并不需要这样做,因为$rand_key已经是数组中的一个键array_rand 返回一个键 !),因此代码$rand_key为:

$rand_key = array_rand($imgArray);
// optional, if you need the value for something:
// $rand_val = $imgArray[$rand_key];
unset($imgArray[$rand_key]);

You are confusing keys and values, that's why. 这就是混淆键和值的原因。 $imgArray is a range(1,9 ) but that's values. $ imgArray是一个range(1,9 ),但这是值。 Your keys are range(0,8) . 您的键是range(0,8)

So *$rand_key* is actually not a random key but a random value. 因此,* $ rand_key *实际上不是随机密钥,而是随机值。

Thus you delete the wrong... key and you're exposes to an OutOfBounds exception. 因此,您删除了错误的...键,并且暴露于OutOfBounds异常。

If you want to randomize searched values, the use array_search to retrieve and process the key 如果要随机化搜索值,请使用array_search检索和处理密钥

If you want to randomize searched keys, then use range(0,8) . 如果要随机搜索关键字,请使用range(0,8)

Try this: 尝试这个:

// Create array
$imgArray = range(1,9);

// Get random key from array
$rand_key = array_rand($imgArray);

// Get key value from array
$imgValue = $imgArray[$rand_key];

// Delete value from array
unset($imgArray[array_search($imgValue,$imgArray)]);


Here is your array:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)

rand_key: 2

imgValue: 3

After delete imgValue: 3

New Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM