取消设置多维数组中的行

发布于 2024-12-02 05:42:41 字数 1224 浏览 1 评论 0原文

我有这个数组 $output ,看起来像这样:

Array(
    [0] => Array(
        [0] => 1a
        [1] => 1b
        [2] => 1c
    )
    [1] => Array(
        [0] => 2a
        [1] => 2b
        [2] => 2c
    )
    [2] => Array(
        [0] => 3a
        [1] => 3b
        [2] => 3c
    )
    [3] => Array(
        [0] => 4a
        [1] => 4b
        [2] => 4c
    )
)

等等...

当我想删除第二个元素时,我只需使用:

$output = unset($output[1]);

得到以下内容:

Array(
    [0] => Array(
        [0] => 1a
        [1] => 1b
        [2] => 1c
    )
    [1] => Array(
        [0] => 3a
        [1] => 3b
        [2] => 3c
    )
    [2] => Array(
        [0] => 4a
        [1] => 4b
        [2] => 4c
    )
)

我的问题是如何删除数组中每个元素的每个第二个元素( [0][1], [1][1], [2][1], [3][1] ,...) 得到以下结果:

Array(
    [0] => Array(
        [0] => 1a
        [1] => 1c
    )
    [1] => Array(
        [0] => 2a
        [1] => 2c
    )
    [2] => Array(
        [0] => 3a
        [1] => 3c
    )
    [3] => Array(
        [0] => 4a
        [1] => 4c
    )
)

I have this array $output which looks like this:

Array(
    [0] => Array(
        [0] => 1a
        [1] => 1b
        [2] => 1c
    )
    [1] => Array(
        [0] => 2a
        [1] => 2b
        [2] => 2c
    )
    [2] => Array(
        [0] => 3a
        [1] => 3b
        [2] => 3c
    )
    [3] => Array(
        [0] => 4a
        [1] => 4b
        [2] => 4c
    )
)

and so on...

When I want to remove the second element I just use:

$output = unset($output[1]);

to get the following:

Array(
    [0] => Array(
        [0] => 1a
        [1] => 1b
        [2] => 1c
    )
    [1] => Array(
        [0] => 3a
        [1] => 3b
        [2] => 3c
    )
    [2] => Array(
        [0] => 4a
        [1] => 4b
        [2] => 4c
    )
)

My question is how to remove every second element of every element in the array ([0][1], [1][1], [2][1], [3][1] ,...) to get the following:

Array(
    [0] => Array(
        [0] => 1a
        [1] => 1c
    )
    [1] => Array(
        [0] => 2a
        [1] => 2c
    )
    [2] => Array(
        [0] => 3a
        [1] => 3c
    )
    [3] => Array(
        [0] => 4a
        [1] => 4c
    )
)

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

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

发布评论

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

评论(6

陈甜 2024-12-09 05:42:41

无法通过简单的命令完成,但可以使用循环:

foreach(array_keys($output) as $key) {
   unset($output[$key][1]);
}

Can't be done with a simple command, but you can use a loop:

foreach(array_keys($output) as $key) {
   unset($output[$key][1]);
}
夕嗳→ 2024-12-09 05:42:41

干净整洁:

$f = function(&$a,$k) { unset($a[1]); };
array_walk($arr, $f);

或:

array_walk($arr, function (&$a, $k) {
  unset($a[1]); 
});

Clean and neat:

$f = function(&$a,$k) { unset($a[1]); };
array_walk($arr, $f);

Or:

array_walk($arr, function (&$a, $k) {
  unset($a[1]); 
});
靑春怀旧 2024-12-09 05:42:41

您可以迭代数组,并在每个子数组中unset()您想要的内容:

foreach($output as &$item) {
    unset($item[2]);
}
unset($item); // unset reference

You can iterate over the array, and unset() what you want in each sub-array:

foreach($output as &$item) {
    unset($item[2]);
}
unset($item); // unset reference
£冰雨忧蓝° 2024-12-09 05:42:41

您可以使用 array_maparray_splice 的组合:

function removeSecond( array &$arr )
{
    array_splice( $arr, 1, 1 );
}
$out = array_map( 'removeSecond', $input );

unset 的问题是它会使索引保持原样:

$ php -r '$arr = array(array(0,1,2)); unset($arr[0][1]); var_dump($arr);'
array(1) {
  [0]=>
  array(2) {
    [0]=>
    int(0)
    [2]=>
    int(2)
  }
}

而 splice 会更新索引:

$ php -r '$arr = array(array(0,1,2)); array_splice($arr[0], 1, 1); var_dump($arr);'
array(1) {
  [0]=>
  array(2) {
    [0]=>
    int(0)
    [1]=>
    int(2)
  }
}

You'd use a combination of array_map and array_splice:

function removeSecond( array &$arr )
{
    array_splice( $arr, 1, 1 );
}
$out = array_map( 'removeSecond', $input );

The problem with unset is that it will leave the indexes as they were:

$ php -r '$arr = array(array(0,1,2)); unset($arr[0][1]); var_dump($arr);'
array(1) {
  [0]=>
  array(2) {
    [0]=>
    int(0)
    [2]=>
    int(2)
  }
}

While splice will update the indexes:

$ php -r '$arr = array(array(0,1,2)); array_splice($arr[0], 1, 1); var_dump($arr);'
array(1) {
  [0]=>
  array(2) {
    [0]=>
    int(0)
    [1]=>
    int(2)
  }
}
梨涡 2024-12-09 05:42:41
foreach($array as $key=>$val){ unset($val[1]); }
foreach($array as $key=>$val){ unset($val[1]); }
苏佲洛 2024-12-09 05:42:41

我用这个:

public function array_unset_key_in_children($array, $keyToUnsetInChildren)
{ 
    $newArray = [];
    foreach($array as $key=>$subArray) { 
        unset($subArray[$keyToUnsetInChild]);
        $newArray[$key] = $subArray;
    }
    return $newArray;
}

I use this:

public function array_unset_key_in_children($array, $keyToUnsetInChildren)
{ 
    $newArray = [];
    foreach($array as $key=>$subArray) { 
        unset($subArray[$keyToUnsetInChild]);
        $newArray[$key] = $subArray;
    }
    return $newArray;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文