php 从多维数组中删除“列”的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16564650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:22:19  来源:igfitidea点击:

Best way to delete "column" from multidimensional array

phparraysarray-splice

提问by Horen

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:

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

array_splice($array, 1, 1);

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

我找到了这种方法,但想知道是否还有一种更简单的方法(类似于行)来删除列?也许先转置数组?

回答by mpyw

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):

编辑(阿玛德):

delete_colfunction 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_spliceinstead of unsetwould not be appropriate in such scenarios (it's offset-based and not key-based).

例如,当您需要遍历数组并在每个步骤中删除一些列时,可以使用此方法。使用array_splice而不是unset 的函数在这种情况下不合适(它基于偏移量而不是基于键)。

回答by Tanathon

The PHP manual for array_walk()states (emphasis added):

状态的 PHP 手册array_walk()(强调):

Only the valuesof the array may potentially be changed; its structure cannot be altered, i.e., the programmer cannot add, unset or reorderelements. If the callback does not respect this requirement, the behavior of this function is undefined, and unpredictable.

只有数组的可能会改变;它的结构不能改变,即程序员不能添加、取消设置或重新排序元素。如果回调不遵守此要求,则此函数的行为是undefinedunpredictable

That sounds to me as if mpyw's and Amade's answermight work but cannot be relied on.

对我来说,这听起来好像mpyw 和 Amade 的答案可能有效,但不能依赖。

A safer solution might be the following:

更安全的解决方案可能如下:

function delete_col(&$array, $key)
{
    // Check that the column ($key) to be deleted exists in all rows before attempting delete
    foreach ($array as &$row)   { if (!array_key_exists($key, $row)) { return false; } }
    foreach ($array as &$row)   { unset($row[$key]); }

    unset($row);

    return true;
}