Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

278
Views
Remove uneven prices from list of price

I have prices array like this

  $arr = [200,201,203,205,207,300,209,212,0,211,0,0,301,213,214];

Above is the list of price , where there are few uneven numbers like 0, 300, 301

I am trying to remove those odd numbers by following code

$fresharray = [];
foreach($arr as $lol)
{
    $diff =  $lol-$arr[$i-1];

    if($diff <-50 || $diff > 50)
    {
 
    }
    else 
    {
     $fresharray[] = $lol;
    }
   
    $i++;
}

I am basically comparing current number with previous number and if difference is more than 50 i am excluding it to my fresharray , but my problem is when there is two 0 like 0,0 or 300,301.. here difference is 0 and -1 and its getting added to my fresharray.How do I remove this odd numbers out of my price array.. please note my array size is usually more than 1000.Solution can be either in php or javascript.Thanks.

please note price can start from 200 and end at 400 too.. but i just wanna check around current number for which is uneven.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First of all, since you say there is a price range, you should surround the inside of the foreach with an if that ignores the values that are outside your range, fixing your issue where a 0 causes the next value to get added to $fresharray:

if ($lol >= 200 && $lol <= 400)

The definition of "latest number" here is ambiguous. From what you said in the question, it seems you mean the previous number in $arr. For that, you just need a value that is assigned the value of $lol at the end of the foreach, so that the next iteration can read it.

Also, put $fresharray[] = $lol; inside the if rather than the else. Right now, the code adds $lol's whose $diff is not <-50 or >50. This is likely why in an array that contains [...,300,301,...] the 301 is added to $fresharray.

This is the resulting code after the changes:

<?php
$arr = [200, 201, 203, 205, 207, 300, 209, 212, 0, 211, 0, 0, 301, 213, 214];
$fresharray = [];
$latest = $arr[0]; //Initialize $latest to avoid warnings
foreach ($arr as $lol) {
    if ($lol >= 200 && $lol <= 400) { //Ignore prices outside range
        $diff =  $lol - $latest;
        if ($diff < -50 || $diff > 50) {
            $fresharray[] = $lol;
        }
        $latest = $lol;
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!