4

Let's say you have an array with numeric keys like

$ar = [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd', 4 => 'e', 5 => 'f', 6 => 'g'];

and a defined offset of 4 ($offset = 4). Now you want to slice a part of that array, starting with the offset.

$slice = array_slice($ar, $offset, null, true);

And you don't just want to keep the original keys, but actually raise them by 1, so the result would be:

Array
(
    [5] => e
    [6] => f
    [7] => g
)

instead of

Array
(
    [4] => e
    [5] => f
    [6] => g
)

Sure, you can loop through the array (foreach, array_walk) and reassign all keys, like:

$new_ar = [];
$raise_by = 1; // Could be any other amount
foreach ($slice as $key => $val) {
    $new_ar[$key + $raise_by] = $val;
}

But is there any way to do it without the additional, external loop and (re)assigning the keys? Something like "slice the array at position x and start keys with x + 1"?

EDIT/Possible solutions:

Inspired by the comments, I see 2 possible solutions in addition to Brian's comment in How to increase by 1 all keys in an array?

Static, short and basic:

array_unshift($ar, 0);
$result = array_slice($ar, $offset + 1, null, true);

More flexible, but probably less performant:

$shift = 1;
$slice = array_slice($ar, $offset, null, true);
$ar = array_merge(range(1, $offset + $shift), array_values($slice));
$result = array_slice($ar, $offset + $shift, null, true);

Advantage is that one can shift the keys by any arbitrary value.

antesoles
  • 683
  • 6
  • 21
  • Your loop could be dangerous. You have to start from the end to add 1 to the keys otherwise, you will overwrite the next value – Alchalade Jul 18 '17 at 10:29
  • Well, that's why I'm assigning it to a new, empty array instead. – antesoles Jul 18 '17 at 10:31
  • 1
    I don't think you will find better or faster than your solution. Look here : https://stackoverflow.com/questions/12715514/how-to-increase-by-1-all-keys-in-an-array – Vincent Decaux Jul 18 '17 at 10:32

4 Answers4

1

You can do this. If you don't want to use loop. use array_combine with empty array range from 5-7

$ar = [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd', 4 => 'e', 5 => 'f', 6 => 'g'];
$offset = 4;
$slice = array_slice($ar, $offset, null, true);
$slice = array_combine(range($offset+1, count($slice)+$offset), array_values($slice));//<--------check this

echo "<pre>";
print_r($slice);
B. Desai
  • 16,414
  • 5
  • 26
  • 47
0

You can get your answer by this,

$ar = [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd', 4 => 'e', 5 => 'f', 6 => 'g'];

$slice = array_slice($ar, '4', null, true);


    $new_arr = array();
    foreach($slice as $key => $val)
    {
        $key = $key + 1;
        $new_arr[$key] = $val;
        $key++;
    }

But, check it once as @Alchalade told. Check if it do not affect your output.

Virb
  • 1,639
  • 1
  • 16
  • 25
0

Insert a new value for the original array at position 0 then slice it so each key will increase by one according to that.

$ar=array_splice($ar,0,0,' ');
$slice = array_slice($ar, $offset, null, true);
Osama
  • 2,912
  • 1
  • 12
  • 15
  • I don't see how this can work with array_splice, but you inspired me for using `array_unshift($ar, 0);` and slicing then with `$offset + 1`. – antesoles Jul 18 '17 at 10:51
  • https://www.w3schools.com/php/func_array_splice.asp This link will show you how array_splice will help you , another way is with array_unshift as you mention. – Osama Jul 18 '17 at 11:04
0
<?php
$ar = array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd', 4 => 'e', 5 => 'f', 6 => 'g');
$ar = array_combine(
        array_map(function ($key) { return ++$key; }, array_keys($ar)),
        $ar
);
$offset = 4;
$sliced = array_slice($ar, $offset, count($ar), TRUE);
echo '<pre>';print_r($sliced);echo '</pre>';
Pupil
  • 23,834
  • 6
  • 44
  • 66