从多维数组中删除“列”的最佳方法[重复]

时间:2022-09-16 14:35:21

This question already has an answer here:

这个问题在这里已有答案:

I have a multidimensional php array that represents a table like this

我有一个多维的PHP数组,代表一个像这样的表

-------------
| A | 0 | A |
|---|---|---|
| 0 | 0 | 0 |
|---|---|---|
| A | 0 | A |
-------------

so the array looks like this:

所以数组看起来像这样:

array (size=3)
  0 => 
    array (size=3)
      0 => string 'A' (length=1)
      1 => string '0' (length=1)
      2 => string 'A' (length=1)
  1 => 
    array (size=3)
      0 => string '0' (length=1)
      1 => string '0' (length=1)
      2 => string '0' (length=1)
  2 => 
    array (size=3)
      0 => string 'A' (length=1)
      1 => string '0' (length=1)
      2 => string 'A' (length=1)

Now i want to delete the second row and the second column (this is just a simplified example btw).
Deleting the row is easy:

现在我想删除第二行和第二列(这只是一个简化的例子btw)。删除行很容易:

array_splice($array, 1, 1);

I found this approach but was wondering if there was a simpler way (similar to the row) of deleting the column as well? Maybe transposing the array first?

我找到了这种方法但是想知道是否有更简单的方法(类似于行)删除列?也许先转换阵列?

1 个解决方案

#1


15  

Try this:

尝试这个:

function delete_row(&$array, $offset) {
    return array_splice($array, $offset, 1);
}

function delete_col(&$array, $offset) {
    return array_walk($array, function (&$v) use ($offset) {
        array_splice($v, $offset, 1);
    });
}

Tested on Ideone: http://ideone.com/G5zRi0

在Ideone上测试:http://ideone.com/G5zRi0

Edit (Amade):

编辑(Amade):

delete_col function can also be slightly modified to work with arrays with missing columns:

也可以稍微修改delete_col函数以使用缺少列的数组:

function delete_col(&$array, $key) {
    return array_walk($array, function (&$v) use ($key) {
        unset($v[$key]);
    });
}

This can be used e.g. when you need to iterate over an array and remove some columns in each step. A function using array_splice instead of unset would not be appropriate in such scenarios (it's offset-based and not key-based).

这可以用于例如当你需要迭代一个数组并删除每一步中的一些列。使用array_splice而不是unset的函数在这种情况下是不合适的(它是基于偏移的而不是基于密钥的)。

#1


15  

Try this:

尝试这个:

function delete_row(&$array, $offset) {
    return array_splice($array, $offset, 1);
}

function delete_col(&$array, $offset) {
    return array_walk($array, function (&$v) use ($offset) {
        array_splice($v, $offset, 1);
    });
}

Tested on Ideone: http://ideone.com/G5zRi0

在Ideone上测试:http://ideone.com/G5zRi0

Edit (Amade):

编辑(Amade):

delete_col function can also be slightly modified to work with arrays with missing columns:

也可以稍微修改delete_col函数以使用缺少列的数组:

function delete_col(&$array, $key) {
    return array_walk($array, function (&$v) use ($key) {
        unset($v[$key]);
    });
}

This can be used e.g. when you need to iterate over an array and remove some columns in each step. A function using array_splice instead of unset would not be appropriate in such scenarios (it's offset-based and not key-based).

这可以用于例如当你需要迭代一个数组并删除每一步中的一些列。使用array_splice而不是unset的函数在这种情况下是不合适的(它是基于偏移的而不是基于密钥的)。