Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP. Removing part of the array

Tags:

arrays

php

It is necessary to remove part of the array. Here is an example of an array:

Array ( [0] => one [1] => two [2] => three [3] => four [4] => five )

The variable can be based on one of the following values ​​in the array. Suggests there is 'three'. Need to take one, two and everything else removed.

Is there any standard methods, or a good solution that would not need to use a loop?

like image 579
Andrei Avatar asked May 13 '26 00:05

Andrei


2 Answers

You can use array_splice for that

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")
like image 80
Asciiom Avatar answered May 14 '26 14:05

Asciiom


If you don't want to use a loop, you could use array_splice.

$input = array("red", "green", "blue", "yellow");
array_splice($input, $varaible, -1);
// $input is now array("red", "yellow")
like image 35
Grim... Avatar answered May 14 '26 13:05

Grim...