noa-dev
noa-dev

Reputation: 3641

PHP if statement error

My question is: Why does the script enter ALL if statements? Even if the "in array" question shouldn't let them.

First I show you the array which I am getting back from my model: In this case the array is just like array([0] => array()) because I only get 1 row back from my DB.

array(1) {
  [0]=>
  array(7) {
    ["Kunden_ID"]=>
    float(250)
    ["Kundentyp_ID"]=>
    float(1)
    ["kundentyp_row_id"]=>
    int(100)
    ["ext_kdnr"]=>
    float(0)
    ["kundentyp_anmerkung"]=>
    string(0) ""
    ["status"]=>
    string(0) ""
    ["name"]=>
    string(4) "ROLA" // Thats what we are looking for in our statements
  }
}

In the following code I need to check whether a specific string is in a specific index of the result array or not. That way I can create relations which are needed later.

Now to the code:

 for($i = 0; $i < $arr_length; $i++){ // for runs only once since we only get 1 row back
        if(in_array("ROLA", $result_array[$i])){ 
// check if you find the string "ROLA" in the array idx $i
            $rola = true;
            $rola_pos = $i;
            $rola_row = $result_array[$i]["kundentyp_row_id"];
            echo "<br/>ROLA TYP AKTIV";
        }
        if(in_array("ROLA_OBC", $result_array[$i])){
            $rola_obc = true;
            $rola_obc_pos = $i;
            $rola_obc_row = $result_array[$i]["kundentyp_row_id"];
            echo "<br/>ROLA OBC TYP AKTIV";
        }
        if(in_array("MYTO_FAI", $result_array[$i])){
            $myto_fai = true;
            $myto_fai_pos = $i;
            $myto_fai = $result_array[$i]["kundentyp_row_id"];
            echo "<br/>MYTO FAI TYP AKTIV";
        }
        if(in_array("OMV", $result_array[$i])){
            $omv = true;
            $omv_pos = $i;
            $omv = $result_array[$i]["kundentyp_row_id"];
            echo "<br/>OMV TYP AKTIV";
        }
        if(in_array("VAT", $result_array[$i])){
            $vat = true;
            $vat_pos = $i;
            $vat = $result_array[$i]["kundentyp_row_id"];
            echo "<br/>VAT TYP AKTIV";
        }
        if(in_array("PLAKETTEN", $result_array[$i])){
            $plaketten = true;
            $plaketten_pos = $i;
            $plaketten = $result_array[$i]["kundentyp_row_id"];
            echo "<br/>PLAKETTEN TYP AKTIV";
        }
        if(in_array("SPEDITION", $result_array[$i])){
            $spedition = true;
            $spedition_pos = $i;
            $spedition = $result_array[$i]["kundentyp_row_id"];
            echo "<br/>SPEDITION TYP AKTIV";
        }
        if(in_array("MOEST", $result_array[$i])){
            $moest = true;
            $moest_pos = $i;
            $moest = $result_array[$i]["kundentyp_row_id"];
            echo "<br/>MOEST TYP AKTIV";
        }
        if(in_array("BERUFUNGEN", $result_array[$i])){
            $berufungen = true;
            $berufungen_pos = $i;
            $berufungen = $result_array[$i]["kundentyp_row_id"];
            echo "<br/>BERUFUNGEN TYP AKTIV";
        }
        if(in_array("DIVERSE", $result_array[$i])){
            $diverse = true;
            $diverse_pos = $i;
            $diverse = $result_array[$i]["kundentyp_row_id"];
            echo "<br/>DIVERSE TYP AKTIV";
        }
    }

As you see the result array contains the string "ROLA" therefore the first IF statement should be true and executed. Which is the case. BUT all other if statements appear to be true as well. Why?

EDIT

As requested here is something you can copy paste into a php file to reproduce it :

    $testArr = array ( 0 => array ( 'Kunden_ID' => 250, 'Kundentyp_ID' => 1, 'kundentyp_row_id' => 100, 'ext_kdnr' => 0, 'kundentyp_anmerkung' => '', 'status' => '', 'name' => 'ROLA', ));

$length = count($testArr);
for($i = 0; $i < $length; $i++ ){
    if(in_array("ROLA", $testArr[$i])){
        $rola = true;
        echo "<br/>ROLA TYP AKTIV";
    }
    if(in_array("ROLA_OBC", $testArr[$i])){
        $rola_obc = true;
        echo "<br/>ROLA OBC TYP AKTIV";
    }
    if(in_array("MYTO_FAI", $testArr[$i])){
        $myto_fai = true;
        echo "<br/>MYTO FAI TYP AKTIV";
    }
    if(in_array("OMV", $testArr[$i])){
        $omv = true;
        echo "<br/>OMV TYP AKTIV";
    }
    if(in_array("VAT", $testArr[$i])){
        $vat = true;
        echo "<br/>VAT TYP AKTIV";
    }
    if(in_array("PLAKETTEN", $testArr[$i])){
        $plaketten = true;
        echo "<br/>PLAKETTEN TYP AKTIV";
    }
    if(in_array("SPEDITION", $testArr[$i])){
        $spedition = true;
        echo "<br/>SPEDITION TYP AKTIV";
    }
    if(in_array("MOEST", $testArr[$i])){
        $moest = true;
        echo "<br/>MOEST TYP AKTIV";
    }
    if(in_array("BERUFUNGEN", $testArr[$i])){
        $berufungen = true;
        echo "<br/>BERUFUNGEN TYP AKTIV";
    }
    if(in_array("DIVERSE", $testArr[$i])){
        $diverse = true;
        echo "<br/>DIVERSE TYP AKTIV";
    }
}

Upvotes: 2

Views: 112

Answers (2)

lightbringer
lightbringer

Reputation: 835

it looks like your array is an associative array, not just a normal array

Please take a look at the manual page http://php.net/manual/en/function.in-array.php

Apparently in_array() goes crazy with associative array

You may want to extract values and use in_array() on values array only

Upvotes: 0

pisamce
pisamce

Reputation: 533

Try to use "strict" flag (third parameter of in_array). For example:

if(in_array("ROLA", $testArr[$i], true)){
    $rola = true;
    echo "<br/>ROLA TYP AKTIV";
}
if(in_array("ROLA_OBC", $testArr[$i], true)){
    $rola_obc = true;
    echo "<br/>ROLA OBC TYP AKTIV";
}
if(in_array("MYTO_FAI", $testArr[$i], true)){
    $myto_fai = true;
    echo "<br/>MYTO FAI TYP AKTIV";
}

Upvotes: 2

Related Questions