17

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
    )
)
Tgr
  • 27,442
  • 12
  • 81
  • 118
Giel
  • 171
  • 1
  • 1
  • 3

6 Answers6

25

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

foreach(array_keys($output) as $key) {
   unset($output[$key][1]);
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    Very helpful, thanks! To add to your answer, if you want to keep your remaining keys sequential after the unset, you can include at the end of (and within) your `foreach`: `$output[$key] = array_values($output[$key]);`. The `array_values()` will rebase the keys so they are sequential starting from zero. – Mike Jan 08 '15 at 21:22
  • If you want to reindex after unsetting, just call `array_splice()` as @cwallen demonstrates. – mickmackusa Jul 31 '21 at 07:34
10

Clean and neat:

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

Or:

array_walk($arr, function (&$a, $k) {
  unset($a[1]); 
});
dnagirl
  • 20,196
  • 13
  • 80
  • 123
8

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
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • 1
    Make sure you include the `&` like arnaud has above, this is required to reference it when you call unset – sMyles Apr 18 '17 at 21:30
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)
  }
}
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
1
foreach($array as $key=>$val){ unset($val[1]); }
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
FabioG
  • 2,936
  • 3
  • 31
  • 50
  • This code-only answer does not affect the original `$array`, but an element within a copy of `$array`. Within `$val`, the `1`-keyed element will be removed, but `$array` is never mutated. Proof: https://3v4l.org/aZ52f – mickmackusa Jul 29 '21 at 10:25
0

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;
}
T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • @mickmackusa thanks, I've removed "array_key_exists" before unset, but about mutation - I had a case (couldnt explain why but it was hundred percent case) that mutating original array ended up using several times more memory (causing "memory exhausted, trying to allocate..." ) while creating new arrays did go incomparably easier. Had been debugging that case for a day, and this ended for sure to me. – T.Todua Jul 31 '21 at 07:28
  • Okay. You might like to explain the benefit of your snippet in your answer. – mickmackusa Jul 31 '21 at 07:30