根据某些标准交换数组中的元素

I am trying to swap elements in my array according to some criteria. For example, my array has "blocks" which have to move together, for example move +1 spot in the array.

// bubble sort
foreach ($cleanCard as $key=>$value) {
    $cleanCard = handleGroupDirection('down', $value['groupID'], $cleanCard);
}

The $cleanCard array passed as parameter above looks like:

$cleanCard = array(
    '0' => array(
            'groupID' => 1,
            'value' => 'T PHONE'
        ),
    '1' => array
        (
            'groupID' => 1,
            'value' => ''
        ),
    '2' => array
        (
            'groupID' => 1,
            'value' => 'C CELL'
        ),
    '3' => null,
    '4' => array
        (
            'groupID' => 0,
            'value' => 'EMAIL'
        )
);

As you can see in this array, the element at key #3 is empty, which means that the group should move down 1 spot (even a grouped item that has no value...). The empty key stays in the array.

The resulting array should look like:

Array
(
    [0] => 
    [1] => Array
        (
            [groupID] => 1
            [value] => T PHONE
        )

    [2] => Array
        (
            [groupID] => 1
            [value] => 
        )

    [3] => Array
        (
            [groupID] => 1
            [value] => C CELL
        )
    [4] => Array
        (
            [groupID] => 0
            [value] => EMAIL
        )

)

My code looks like:

function handleGroupDirection($direction, $groupID, $card) {
    $groupArr = array_map(function($field) { return $field['groupID']; }, $card);
    $groupKeys = array_keys($groupArr, $groupID);
    $firstFieldOfGroupIndex = $groupKeys[0];
    $lastFieldOfGroupIndex = end($groupKeys);

    switch ($direction) {
        case 'down':
            if ($lastFieldOfGroupIndex+1 >= count($card)) break;

            if ($card[$lastFieldOfGroupIndex+1] == null) {
                // move each field of the group down, starting
                // from the last field in the group

                for ($i = count($groupKeys); $i >= 0; $i--) {
                    $currentCardIndex = $groupKeys[$i];
                    $card[$currentCardIndex+1] = $card[$currentCardIndex];
                }

                // make the first field of the group null, the null spot we took is now basically going in front of the group
                $card[$firstFieldOfGroupIndex] = null;

            }

            break;
    }

    return $card;
}

Haven't had success with this code yet.

Edit: I get the notices:

NOTICE Undefined offset: 3 on line number 20
$currentCardIndex = $groupKeys[$i];
NOTICE Undefined index: on line number 21
$card[$currentCardIndex+1] = $card[$currentCardIndex];

And the current array I get is:

Array
(
    [0] => 
    [1] => Array
        (
            [groupID] => 1
            [value] => T PHONE
        )

    [2] => 
    [3] => Array
        (
            [groupID] => 1
            [value] => C CELL
        )

    [4] => Array
        (
            [groupID] => 0
            [value] => EMAIL
        )

)

[2] Shouldn't be empty !