18

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
    )
)
1
  • 1
    unset() doesn't return anything. Commented Aug 31, 2011 at 17:18

6 Answers 6

26

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

foreach(array_keys($output) as $key) {
   unset($output[$key][1]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
If you want to reindex after unsetting, just call array_splice() as @cwallen demonstrates.
12

Clean and neat:

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

Or:

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

1 Comment

Why declare $k? You never use it.
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

1 Comment

Make sure you include the & like arnaud has above, this is required to reference it when you call unset
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)
  }
}

Comments

1
foreach($array as $key=>$val){ unset($val[1]); }

1 Comment

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: 3v4l.org/aZ52f
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;
}

2 Comments

@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.
Okay. You might like to explain the benefit of your snippet in your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.