3

I have array with data and key [1, 2, 4, 5].

$array = xxxx;

this is:

                       [array] => Array
                            (
                                [1] => Array
                                    (
                                        [test] => fdasdfs24
                                    )

                                [2] => Array
                                    (
                                        [test] => fdasdf23
                                    )

                                [4] => Array
                                    (
                                        [test] => fdasdf4
                                    )

                                [5] => Array
                                    (
                                        [test] => fdasdf34
                                    )

                            )

How can i reset indexes in this and array? I would like receive: [1, 2, 3, 4]

                       [array] => Array
                            (
                                [1] => Array
                                    (
                                        [test] => fdasdfs24
                                    )

                                [2] => Array
                                    (
                                        [test] => fdasdf23
                                    )

                                [3] => Array
                                    (
                                        [test] => fdasdf4
                                    )

                                [4] => Array
                                    (
                                        [test] => fdasdf34
                                    )

                            )

how can i make it? Maybe is for this some function? I dont want doing this mannually - this is only example.

4
  • 2
    possible duplicate of Reset PHP Array Index Commented Jan 18, 2013 at 15:50
  • What are you really trying to do here? I believe the goal is to reset the keys, starting with an index of 1. Commented Jan 18, 2013 at 15:52
  • @deceze no its not exact duplicate. Its resetting array index to 1 instead of 2. Thats a big difference. Commented Jan 18, 2013 at 15:57
  • Could you post the array in var_export format? That way other can actually run their test code right off of your array. Commented Jan 18, 2013 at 16:01

4 Answers 4

6

As you index starts as 1 instead of 0, you need to use range function and with the help of array_combine you get the resultant array.

array_combine(range(1,4),  array_values($array));

To make it more general you can use this,

array_combine(range(1,count($array)),  array_values($array));
Sign up to request clarification or add additional context in comments.

1 Comment

+ for interesting. Albeit very specific to the data set.
5
$new_array = array_values($old_array);

2 Comments

Note his index starts at 1.
@JasonMcCreary thats what my answer fixes!
3

Just add an element to the beginning, rekey and then shift it off.

$array = array_values(array_unshift($array, 'blank'));
unset($array[0]);

I didn't test it but that should do it.

Comments

2

One way to do it is:

$newArray = array();
foreach($oldArray as $val):
    $newArray[] = $val; //add the value to the new array
endforeach;

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.