20

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

-------------
| 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 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?

Community
  • 1
  • 1
Horen
  • 11,184
  • 11
  • 71
  • 113
  • 2
    The first comment on the accepted answer to the question your posted says it all really. – George May 15 '13 at 12:13
  • That answer is 4 lines, can't get much simpler than that. It only uses the function you yourself provided combined with a foreach loop... – dtech May 15 '13 at 12:24
  • 1
    well, let me rephrase: is there a better performing way? – Horen May 15 '13 at 12:26
  • 3
    THIS IS NOT A DUPLICATE. This question is "delete" a column. The other question is how to return x columns. These are different questions. – John Ballinger Aug 21 '14 at 03:58

5 Answers5

24

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

Edit (Amade):

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

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

Amade
  • 3,665
  • 2
  • 26
  • 54
mpyw
  • 5,526
  • 4
  • 30
  • 36
  • Cool. Sadly I can't google the way to find and delete empty columns. Any premade solution? – Hebe Mar 21 '22 at 22:42
6

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

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

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

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;
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Tanathon
  • 61
  • 1
  • 4
3

You can apply unset() on the column on each row:

foreach ($array as &$row) {
    unset($row['column']);
}

And $array doesn't contain the column anymore.

Wadih M.
  • 12,810
  • 7
  • 47
  • 57
0

based on what i saw here a more complete function to delete recursively (or not) one or more columns in a array:

function array_columns_delete(&$array, $keys,$recursive=false) {
    return array_walk($array, function (&$v) use ($keys,$recursive) {
        foreach((array)$keys as $key) {
            if (isset($v[$key])) unset($v[$key]);
            if ($recursive==true){
                foreach($v as $k=>&$s){
                    if (is_array($s)) array_columns_delete($s, $keys,$recursive);
                }                
            }
        }
    });
}
emmanuel
  • 133
  • 3
0

Not 100% on the re-indexing by the array_values on a multi-dimensional array. Otherwise this should be a simple solution.

$array = array() // your array above    
foreach ( range(0, count($array)-1) as $i ) {
        unset($array[$i][1]);
        // optional - re-index the array
        $array[$i] = array_values($array[$i]);
    }
wbrco01
  • 1
  • 1