如何在对数字键重新编号时从数组中删除值

发布于 2024-08-17 08:13:06 字数 628 浏览 2 评论 0原文

我有一个可能包含数字或关联键或两者的数组:

$x = array('a', 'b', 'c', 'foo' => 'bar', 'd', 'e');
print_r($x);
/*(
    [0] => a
    [1] => b
    [2] => c
    [foo] => bar
    [3] => d
    [4] => e
)*/

我希望能够从数组中删除一个项目,对非关联键重新编号以保持它们的顺序:

$x = remove($x, "c");
print_r($x);
/* desired output:
(
    [0] => a
    [1] => b
    [foo] => bar
    [2] => d
    [3] => e
)*/

找到要删除的正确元素不是问题,而是有问题的钥匙。 unset 不会对键重新编号,并且 array_splice 适用于偏移量,而不是键(即:从第一个示例中获取 $x ,array_splice($x, 3, 1) 将删除“bar”元素而不是“d”元素)。

I have an array which may contain numeric or associative keys, or both:

$x = array('a', 'b', 'c', 'foo' => 'bar', 'd', 'e');
print_r($x);
/*(
    [0] => a
    [1] => b
    [2] => c
    [foo] => bar
    [3] => d
    [4] => e
)*/

I want to be able to remove an item from the array, renumbering the non-associative keys to keep them sequential:

$x = remove($x, "c");
print_r($x);
/* desired output:
(
    [0] => a
    [1] => b
    [foo] => bar
    [2] => d
    [3] => e
)*/

Finding the right element to remove is no issue, it's the keys that are the problem. unset doesn't renumber the keys, and array_splice works on an offset, rather than a key (ie: take $x from the first example, array_splice($x, 3, 1) would remove the "bar" element rather than the "d" element).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如何视而不见 2024-08-24 08:13:06

这应该在保留字符串键的同时重新索引数组:

$x = array_merge($x);

This should re-index the array while preserving string keys:

$x = array_merge($x);
烟花易冷人易散 2024-08-24 08:13:06

您可以使用下一个优雅的解决方案进行修复:

例如:

<?php

$array = array (
    1 => 'A',
    2 => 'B',
    3 => 'C'
);

unset($array[2]);

/* $array is now:
Array (
    1 => 'A',
    3 => 'C'
);
As you can see, the index '2' is missing from the array. 
*/

// SOLUTION:
$array = array_values($array);

/* $array is now:
Array (
    0 => 'A',
    1 => 'C'
);
As you can see, the index begins from zero. 
*/
?>

You can fixet with next ELEGANT solution:

For example:

<?php

$array = array (
    1 => 'A',
    2 => 'B',
    3 => 'C'
);

unset($array[2]);

/* $array is now:
Array (
    1 => 'A',
    3 => 'C'
);
As you can see, the index '2' is missing from the array. 
*/

// SOLUTION:
$array = array_values($array);

/* $array is now:
Array (
    0 => 'A',
    1 => 'C'
);
As you can see, the index begins from zero. 
*/
?>
两相知 2024-08-24 08:13:06

我想出了这个 - 尽管我不确定它是否是最好的:

// given: $arr is the array
//        $item is the item to remove

$key = array_search($item, $arr);  // the key we need to remove
$arrKeys = array_keys($arr);
$keyPos = array_search($key, $arrKeys); // the offset of the item in the array

unset($arr[$key]);
array_splice($arrKeys, $keyPos, 1);

for ($i = $keyPos; $i < count($arrKeys); ++$i) {
    if (is_int($arrKeys[$i])) --$arrKeys[$i]; // shift numeric keys back one
}
$arr = array_combine($arrKeys, $arr);  // recombine the keys and values.

为了简洁起见,我省略了一些内容。例如,在使用上述代码之前,您需要检查数组是否是关联的,以及要删除的键是否是字符串。

I've come up with this - though I'm not sure if it's the best:

// given: $arr is the array
//        $item is the item to remove

$key = array_search($item, $arr);  // the key we need to remove
$arrKeys = array_keys($arr);
$keyPos = array_search($key, $arrKeys); // the offset of the item in the array

unset($arr[$key]);
array_splice($arrKeys, $keyPos, 1);

for ($i = $keyPos; $i < count($arrKeys); ++$i) {
    if (is_int($arrKeys[$i])) --$arrKeys[$i]; // shift numeric keys back one
}
$arr = array_combine($arrKeys, $arr);  // recombine the keys and values.

There's a few things I've left out, just for the sake of brevity. For example, you'd check if the array is associative, and also if the key you're removing is a string or not before using the above code.

梦在深巷 2024-08-24 08:13:06

尝试 array_diff() 它可能无法正确排序新数组
如果不是,以下应该可以工作,

您将需要在删除函数中迭代它。

function remove($x,$r){
 $c = 0;
 $a = array();
 foreach ($x as $k=>$v){
   if ($v != $r) {
     if (is_int($k)) {
       $a[$c] = $v;
       $c++;
     }
     else {
       $a[$k] = $v;
     }
   } 
 }
 return $a;
}

直流

Try array_diff() it may not order the new array correctly though
if not the following should work

You will need to iterate over it in the remove function.

function remove($x,$r){
 $c = 0;
 $a = array();
 foreach ($x as $k=>$v){
   if ($v != $r) {
     if (is_int($k)) {
       $a[$c] = $v;
       $c++;
     }
     else {
       $a[$k] = $v;
     }
   } 
 }
 return $a;
}

DC

oО清风挽发oО 2024-08-24 08:13:06

我认为这个问题没有一个优雅的解决方案,您可能需要循环到数组并自己重新排序键。

I don't think there is an elegant solution to this problem, you probably need to loop to the array and reorder the keys by yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文