1

I need some help, even though I think I'm checking for the length of the array and I should be breaking out of the loop, I still get warnings on my [else if ($value....] line. So either I'm missing something crucial or I've been staring at this code segment too long and its obvious. Any insight would be appreciated.

    $count = count($filter); //Filter is an array
    if ($count > 1 ){
        //Compare values and generate a range to choose from
        $i = 1;
        foreach($filter as $value){
            //Break the loop if at the end of the array
            if ($i >= $count){
                //throw new exception($i .' '.$count);
                break;
            }
            //if the value is smaller then the next procceding value, because they are already in order of presidence,
            //add it to our range of potentials.
            else if($value < $filter[$i]->value){
                array_push($range, key($filter));
            }
        $i++;
        }           
    }else {
        return false;
    }
3
  • Show the output of var_dump($filter);. If there are any gaps in your array or if it's an associative array (string indexes), this will happen. Commented Jun 11, 2012 at 18:44
  • What is the warning you get on else if ($value... ? Commented Jun 11, 2012 at 19:21
  • Notice: Undefined offset: 1 in //yadada file// line # - but the answers were right. My keys aren't sequential, derp. Thanks guys. Commented Jun 11, 2012 at 19:49

3 Answers 3

1

I suspect that there are gaps in your array. Try this:

$filter = array_values($filter); // this will remove any gaps in the array
$count = count($filter);
if ($count <= 1)
    return false;

for ($i = 0; $i < $count; $i++)
{
    if ($i != $count-1 && $filter[$i]->value < $filter[$i+1]->value)
        array_push($range, key($filter));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I should have known better.
1

Your array might have non-numeric keys. Then try this:

foreach($filter as $key=>$value)
{
   // test for $filter[$key];
}

Or your $filter array doesn't hold objects, then you can't use the -> in

$filter[$key]->value

Comments

0

try this code.... no need of checking count..

$range = array();
$i = 1;
foreach($filter as $value)
{
    if(isset($filter[$i]) && $value < $filter[$i]->value)
    {
        array_push($range, key($filter));
        $i++;
    }
    else
    {
        break;
    }
}

2 Comments

I like this answer, but my derp moment was pointed out earlier though. So you have my thanks.
If you don't check count, then on the last iteration, you will be comparing the last entry in the array with an undefined index of the array. That's why, in his example, he checks if ($i >= $count) { break; }.

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.