-1

I'm working with symfony2, and i really don't understand what's happen with this. i have a == test, who don't return true when he should !At the very first iteration of that foreach, the == 's for test answer one time true, but after the first foreach iteration he don't find the other match... I had tried a lot of var_dump, and the var_dump say that: $service->getId() is int(24), and $discountsID is int(24), but the == test isn't true.

So help me, i'm pretty noob with php, and i really don't get what's happen there..

foreach ($services as $service) {
    for ($i = 0; $i < count($discountsID); ++$i) { 
        if ($service->getId() == $discountsID[$i]) { //the fail test..
            $bool = $discounts[$i]->getId();
        } else {
            $bool = -1;
        }
    } 

    $view_data['services'][] = array(
        'discountId' => bool,
    );
}
Emii Khaos
  • 9,983
  • 3
  • 34
  • 57

2 Answers2

2

You may also want to include a condition in your for that terminates the loop where your if condition is reached. Many persons don't like break (neither I do) but this would work:

if( $service->getId() == $discountsID[$i] ){ //the fail test..
    $bool = $discounts[$i]->getId();      
    break;
}
Hernan Velasquez
  • 2,770
  • 14
  • 21
0

Above the test, add this:

var_dump($service->getId()); 
var_dump($discountsID[$i]); 
die();

And see if the outcome matches

Also this seems wrong to me:

foreach ($services as $service) {
$service->getId()

What does $services consist of?

Richard
  • 4,341
  • 5
  • 35
  • 55
  • yes, bool is $bool in my project. I have done those tests, and i have seen match answer. But the == return false.. $services is a list of some service. $discounts is a list of discount. And one service can have one discount. I want to give to this array those discount's ID (if the service have one) – Pierre-Emmanuel Mercier Jun 03 '13 at 18:23
  • If you have seen a 100% match, then the if statement won't be wrong. Like said above, `'discountId' => bool,` should be `'discountId' => $bool,`. This could be the error. – Richard Jun 03 '13 at 18:26