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
                    )
3
  • How are you removing the value from the array? Commented Jun 27, 2012 at 22:53
  • How are you iterating through the array and displaying it? Commented Jun 27, 2012 at 22:54
  • Use array_shift to remove the first value, the numbers will be re-indexed as you use numeric keys. Commented Jun 27, 2012 at 22:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Sometimes the edit may come another position at present it's 0th position so only unset is applicable for me.
@user1390436 Actually array_splice() will let you remove elements from the array at the nth position, and reorder the keys in one operation - I have edited my answer.
which is exactly what I suggested initially :)
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.