0

Each row of SQL request returns multi-dimentional array with only 1 element "active" now when I do this it outputs even part of array that is "0" as well

for ($i = 0; $i < count($sql_response); $i++) {
    foreach ($sql_response[$i] as $key => $value) {
        if ($key == "active"){
            echo "<br>".$key." - ".$value;
        };
    };
};

If I use === in IF it works, why it does not work with == it seems like i never had this problem with single denominational arrays.

2
  • I nelieve you mean "Each row of sql RESPONSE" Commented Aug 12, 2011 at 0:34
  • Which programming language is this? It sure doesn't look like SQL. Commented Aug 12, 2011 at 0:37

1 Answer 1

1

If $sql_response contains something like:

array(
    0 => "value",
    "active" => "value"
)

then the foreach statement will go through both elements, first assigning 0 to $key, then assigning "active" to key.

$key == "active" will return true when $key is 0, because:

  • it compares 0 == "active"
  • which casts "active" to integer so it can be compared with 0
  • which results in 0 == 0
  • which is true

If you're using the mysql_* or mysqli_* functions to obtain $sql_response, then to prevent adding the numeric keys to the result array, use mysql_fetch_assoc or mysqli_fetch_assoc instead of their *_fetch_array counterparts.

Sign up to request clarification or add additional context in comments.

1 Comment

nice... I never know that it would cast a string of letters to a number in this case... this is good to learn.

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.