0

In the PHP function range there are a start point, a end point and a step point.

is it possible to create an array with numbers, in which some values should not exist?

$hundred_tens = range(120, 190, 10);    

I need the numbers 220,230,...290 ... 920,930..990, but not 200,210 ...900,910.

My solution:

$hundreds = range(100, 800, 100); 
foreach ($hundred_tens as $value) {
    $add_numbers[] = $value + $hundreds[0];
    $add_numbers[] = $value + $hundreds[1];
    $add_numbers[] = $value + $hundreds[2];
    $add_numbers[] = $value + $hundreds[3];
    $add_numbers[] = $value + $hundreds[4];
    $add_numbers[] = $value + $hundreds[5];     
    $add_numbers[] = $value + $hundreds[6];
    $add_numbers[] = $value + $hundreds[7];
}
$all_hundred_tens = array_merge($hundred_tens, $add_numbers);

I have add in a foreach every array value $hundreds to in a new array and merge this array with $hundred_tens.

2 Answers 2

1

You can either loop through your entire range and exclude the unnecessary numbers

$hundred_tens = array();
foreach (range(100, 800, 10) as $number) {
    if ($number % 100 !== 0 && $number % 100 !== 10) {
        $hundred_tens[] = $number;
    }
}

or you can merge smaller ranges:

$hundred_tens = array_merge(range(120, 190, 10), range(220, 890, 10), range(920, 990, 10));

Which one is better depends on how many exclusions you have, and if they are together in the list.

Ref my comment below about for ( being faster, here's an example (only one line is different)

$hundred_tens = array();
for ($number = 100; $number <= 1000; $number += 10) { 
    if ($number % 100 !== 0 && $number % 100 !== 10) {
        $hundred_tens[] = $number;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, so I have to merge it to 7 ranges like that? I don't want to have 300,310,400,410,500,510,600,610,700,710,800,810,too.
Then it will be better to just do one range and check the numbers. I'll update the answer
Note that for ($number = 100; $number <= 800; $number += 10) { will run a lot faster while doing exactly the same thing, but for this small range you won't see a difference.
Ok, but I need later a bigger amount of numbers in an array like this with more exceptions. It would be great, if you could show me an example with a for loop with also fill all needed numbers in an array. (end of the array should be 990 or 1000 in the first code)
1

Basically, you create a for-loop starting from 220 till 910 in steps of 10. In the loop you check whether the number is a hundred or a hundred plus ten. If it is not, then add the number to the array.

To check whether a number is divisable by 100, you could use the module operator %.

So, a % 100 == 0 will result in true, if a is divisable by 100 and false otherwise. For hundreds plus ten, you could do: (a - 10) % 100 == 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.