Reputation: 61
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
)
Upvotes: 0
Views: 527
Reputation: 88647
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.
Upvotes: 6
Reputation: 5115
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.
Upvotes: 3