0

I have got an array

$array = array(
0=>'c'
1=>'a',
2=>'b',
3=>'d',
);

Now, i am going to unset the 1 and 2 keys.

unset($array[1]); unset($array[2]);

How to do if i want an array

array(0=>'c', 1=>'d');

3 Answers3

1

Try

unset($array[1]);
unset($array[2]);
$array = array_values($array);

This works because array_values returns the array you gave, but with increasing keys starting at 0.

Cam
  • 14,930
  • 16
  • 77
  • 128
1

Try This:

$array = array_values($array);

spsaravananct
  • 392
  • 2
  • 4
  • 17
0

Have you tired to use something like that ?

$myarray [0] a->1
         [1] a-7 b->3
         [3] a-8 b->6
         [4] a-3 b->2

array_values

$myarray = array_values($myarray);

btw here is a good answer posted

Community
  • 1
  • 1
Mingebag
  • 748
  • 2
  • 20
  • 35