0

I have a string:

$string = 'Index 1 - Index 2 - Index 3 - Index 4 - Index 5';

So I use: $string2 = explode(" - ",$string);

I would like to get Index 3 to the last index:

Index 3 - Index 4 - Index 5....

Like: $newstring = $sring2[3-end($string2)];

How to do this?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262

1 Answers1

1

Use array_slice() to get part of an array. You pass it the array you want part of and the position you want to start it (minus one since arrays are zero-based keys).

$string = 'Index 1 - Index 2 - Index 3 - Index 4 - Index 5';
$array  = explode(" - ", $string);
$pos = 3;
$newarray = array_slice($array, $pos-1);

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Just add one more parameter to array_slice which is the number of elements to include in the new array: `array_slice($array, $pos-1, 1);` – John Conde Sep 30 '14 at 20:46