DenisZ
DenisZ

Reputation: 331

PHP How to fill up variable with range() and extra numbers


I need to fill up an variable with *range* and some extra numbers. So my variable should have numbers `101-167, `201-245` + `255`. For one range is easy, but can't figure out to add extra numbers:

$i = range(101,167) ??

Then I also have a case that I need to remove few numbers 301-351, but take out 329 and 340. Do I just make several ranges like 301-218, 330-339, 341-351?
Thanks

Upvotes: 0

Views: 69

Answers (1)

Barmar
Barmar

Reputation: 781004

Use array_merge() to concatenate multiple arrays.

$multi_ranges = array_merge(range(101, 167), range(201, 245), [255]);

You can use array_diff() to remove a list of values from the range:

$range_without = array_diff(range(301, 351), [329, 340]);

You can combine these arbitrarily.

$ranges = array_merge(
    range(101, 167), 
    range(201, 245), 
    [255], 
    array_diff(range(301, 351), [329, 340])
);

Upvotes: 2

Related Questions