2

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";
    }
}
9
  • Pretty sure you want to use in_array("whatever", $result_array), not $result_array[$i]... Commented May 28, 2015 at 6:24
  • 2
    Not related to your bug but you should defiantly use a loop, this code has so many repetitions that it just screams it :) Commented May 28, 2015 at 6:29
  • @EdCottrell I don't want to check if its in the WHOLE array. I need to see if its in the particular subarray. Because if I get into the result_array[1] for example then I know that all other entries are related to the STRING im looking for. If i simply check if its in the whole array I cant really make any specific relations Commented May 28, 2015 at 6:33
  • @noa-dev well, that's not clear from your question. You have posted a of of code, but much of it is irrelevant or redundant. Try simplifying and posting a Minimal, Complete, and Verifiable example. Commented May 28, 2015 at 6:37
  • @EdCottrell Thanks for your feedback. I have removed the irrelevant parts Commented May 28, 2015 at 6:42

2 Answers 2

2

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";
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.