The task is to delete a column or row in a two-dimensional array completely, so that the next one would be replaced and the length would be reduced by 1. I tried array_splice(); without specifying replacement but the length of the array is not reduced, and when using sort(); the data does not go to the beginning (up if the line), but to the end - down, after which, when outputting, the first line is the empty line.

Is there some working way to properly trim an array?

Now I do this (input array - unset - output):

 for ($i=0; $i<10; $i++) { for ($j=0; $j<10; $j++) { $hh[$i][$j]="$i$j"; echo $hh[$i][$j]." "; } echo "<br>"; } //array_splice($hh[1],0,10); unset($hh[5]); for ($i=0; $i<10; $i++) { for ($j=0; $j<10; $j++) { echo $hh[$i][$j]." "; } echo "<br>"; } 

Conclusion:

 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 

As a result, the problem is the same, the empty string on the same index. Initially, array_splice($hh[5],0,10); .

  • Give an example of an array, how you try to delete a row / column, the expected result and what you get - Dmitriy Simushev
  • @Dmitriy Simushev updated. - Telion
  • I seem to understand. I bring the line break again. But I thought PHP would scold if you step on a non-existent index. Ie the index of the 10th element still exists or I do not understand anything in arrays. - Telion

4 answers 4

Here are 2 functions for cleanly deleting a row or column in a two-dimensional array with numeric indices

 function array_row_remove($array, $row_index) { if (is_array($array) && array_key_exists($row_index, $array)) { unset($array[$row_index]); $array = array_values($array); } return $array; } function array_col_remove($array, $col_index) { if (is_array($array) && count($array)) { foreach ($array as $row_index => $row) { if (array_key_exists($col_index, $row)) { unset($array[$row_index][$col_index]); $array[$row_index] = array_values($array[$row_index]); } } } return $array; } 
  • Perfect, thank you. - Telion

There is no problem with the code that deletes the string, but there is an output code.

The problem is that after doing

 unset($hh[5]); 

in the array there are no more lines with index 5 . However, when you withdraw, you still refer to it. If you configured PHP logging to output comments, you would get something like:

PHP Notice: Undefined offset: 5 in ...

Solving the problem is trivial: you need to re-index the array. And this can be done, for example, as follows:

 unset($hh[5]); $hh = array_values($hh); 

Well, instead of the hard-wired number of rows / columns, it would be better to use the count function:

 for ($i=0; $i < count($hh); $i++) { for ($j=0; $j < count($hh[$i]); $j++) { echo $hh[$i][$j]." "; } echo "<br>"; } 
  • Thank you very much, now everything is clear. - Telion

If I understand you correctly, you need the unset function. It will remove the string from the array using the passed key. Example of use:

 unset($arrayExample[0]); 

As a result, the value corresponding to key 0 together with the key itself will be deleted from your array.

  • I updated the question, added a conclusion to it which is the problem. How to avoid a blank line? And 2) how to delete a column in this way? - Telion
  • Dmitry gave the correct answer below, I will not duplicate. I think that this is the solution to your problem. - kover-samolet

The answer has already been described by other people, but I found another option, although I don’t fully understand why it worked, but nonetheless.

Of course, you can also output the re-indexed array, but you can go through all the elements that remain:

 for ($i=0; $i<10; $i++) { for ($j=0; $j<10; $j++) { $hh[$i][$j]="$i$j"; echo $hh[$i][$j]." "; } echo "<br>"; } echo "<hr>"; unset($hh[5]); //$hh = array_values($hh); foreach ($hh as $i => $value) { foreach ($hh[$i] as $j => $value1) { echo($hh[$i][$j])." "; } echo "<br>"; } 
  • Using foreach you iterate only existing keys. Using for you choose, by passing other, no longer existing keys. - Dmitriy Simushev