1

I have got the following array.

Array
        (
            [1] => a
            [2] => b
            [3] => c
            [4] => d
            [5] => e
            [6] => f
            [7] => g
            [8] => h
            [9] => i
            [11] => j
            [13] => k
            [14] => l
            [15] => m
            [16] => n
            [17] => o
            [18] => p
            [19] => q
            [20] => r
            [21] => s
            [22] => t
            [23] => u
            [24] => v
            [25] => w
        )

as you can see number 10 and 12 don't exist. Is there a function, so that my array has a range from 1 to 23? So that it would look like this?

Array
        (
            [1] => a
            [2] => b
            [3] => c
            [4] => d
            [5] => e
            [6] => f
            [7] => g
            [8] => h
            [9] => i
            [10] => j
            [11] => k
            [12] => l
            [13] => m
            [14] => n
            [15] => o
            [16] => p
            [17] => q
            [18] => r
            [19] => s
            [20] => t
            [21] => u
            [22] => v
            [23] => w
        )

I hope you understand my problem. Thanks for any help.

5 Answers 5

11

If you want a zero-indexed array:

$new = array_values($array);

If you want to start with an aribitrary number (in this case: 1);

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

Comments

3

What about range() ?

$array = range('a', 'w');

Update: oh, you wan't from 1 to 23. Here you go:

$array = array_combine(range(1,23), range('a', 'w'));

Comments

2

This snippet will do:

$new = Array();
$i = 1;
foreach ($original as $element) {
    $new[$i] = $element;
    $i++;
}

Comments

0

You could use array values to get the full list of keys assuming that its ok for the indexes to run from 0->22 as opposed to 1->23.

If the indexes need to begin at 1 then add an extra step to shift the indexes forward:

$newArray = array_values($array);

$startAtIndex1Array = array();
//shift indexes to start at 1
foreach($newArray as $key=>$value) {
    $startAtIndex1Array[$key+1] = $value;
}

Comments

-1
$tmpArray = Array
        (
            [1] => a
            [2] => b
            [3] => c
            [4] => d
            [5] => e
            [6] => f
            [7] => g
            [8] => h
            [9] => i
            [11] => j
            [13] => k
            [14] => l
            [15] => m
            [16] => n
            [17] => o
            [18] => p
            [19] => q
            [20] => r
            [21] => s
            [22] => t
            [23] => u
            [24] => v
            [25] => w
        );

foreach($tmpArray as $v){
  $tmpArray2[count($tmpArray2)] = $v;
}

It will do what you want but you should consider starting your arrays by a key = 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.