0

I have a problem like,array with 0 10 indexes and i removed 0th index now it's like 1 to 10 when i display this array there is an empty column in 0th position.I want to rearrange this array to 0 to 9. The purpose is the array construct my grid and 0th position is for edit ,if a user doesn't have the permission then i unset the 0th element. How do i rearrange my array to 0 to 9

Sample array structure is like

[fields] => Array
    (
        [1] => Array
            (
                [header] => Array
                    (
                        [fieldName] => OFFERID
                        [displayName] => History
                        [width] => 70
                    )

                [visibility] => Array
                    (
                        [showOnStart] => 1
                        [editable] => 
                    )

                [cell] => Array
                    (
                        [type] => raw
                        [params] => Array
                            (
                                [text] => <a href="javascript:void(0);" id="{{OFFERID}}" class="btnHistory">History</a>
                            )

                    )

                [sort] => Array
                    (
                        [sortable] => 1
                    )

                [validator] => Array
                    (
                        [name] => 
                        [params] => 
                    )

            )

        [2] => Array
            (
                [header] => Array
                    (
                        [fieldName] => OFFERID
                        [displayName] => Offer Id
                        [group] => 0
                    )

thanks Alex

user1390436
  • 61
  • 1
  • 3
  • 6

2 Answers2

6

There are a few ways to do this, here are the two shortest ones (array_values()/array_merge()):

$array = array_values($array);
// or
$array = array_merge($array);

But you might think about using array_shift()/array_pop()/array_splice() to remove the item from the the array instead of (presumably) unset()ing it - these will automatically shift all the keys into a contiguous 0-indexed order.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • Sometimes the edit may come another position at present it's 0th position so only unset is applicable for me. – user1390436 Jun 27 '12 at 22:58
  • @user1390436 Actually [`array_splice()`](http://php.net/manual/en/function.array-splice.php) will let you remove elements from the array at the nth position, and reorder the keys in one operation - I have edited my answer. – DaveRandom Jun 27 '12 at 23:00
  • which is exactly what I suggested initially :) – Dvir Jun 27 '12 at 23:14
  • @DvirAzulay Indeed it is, and you have my +1 :-) – DaveRandom Jun 27 '12 at 23:15
3

You could use array_splice to remove a portion of the array.

array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement ]] )

For example:

$array = array(0 => "a", 1 => "b", 2 => "c");
array_splice($array, 0, 1);
var_dump($array);

Will output:

array(2) {
  [0]=> string(1) "b"
  [1]=> string(1) "c"
}

Notice it will re-index your array, as you requested.

Dvir
  • 5,049
  • 1
  • 23
  • 32