0

If I do not use MySQL, the PHP in_array function works as expected. In this example, the if statement would execute and display the array contains the keyword blue because the in_array function is able to find keyword blue in the array.

<?php
$keyword = "blue";
$array = array("red", "green", "blue", "yellow");

if (in_array($keyword, $array)) {echo "the array contains keyword $keyword";}
else {echo "the array does not contain keyword $keyword";}
?>

in_array fails to do the same when selecting red, green, blue, yellow from MySQL. In this example, if I echo $array, red green blue yellow is displayed in the Web browser, so I know blue is in the $array variable. However, the else statement executes and displays the array does not contain keyword blue.

<?php
$keyword = "blue";

$con = new mysqli('domain','username','password','database');
$sql = "select * from colors";
$sql_query = $con->query($sql);

while ($row = mysqli_fetch_array($sql_query)) {
  $array = $row['colors'];

  if (in_array($keyword, $array)) {echo "the array contains keyword $keyword";}
  else {echo "the array does not contain keyword $keyword";}
}
?>

If anyone has any tips or suggestions here, I certainly would appreciate it.

1
  • Thanks for the tip! I am able to successfully get the "if" statement to trigger with array_key_exists when not using MySQL. However, array_key_exists is still causing the "else" statement to trigger when getting the $array using MySQL. Thanks for the suggestion here! :) Commented Jan 17, 2016 at 1:07

2 Answers 2

2

In your SQL version you get a record where a field named colors should exist.
But this field contains a string (maybe a comma-separated list, something like blue,red,...), not an array.

So when you execute in_array($keyword, $array) you fire a PHP error (but likely you have not currently error_report set to show them).

What you must do is to transform the content you got in colors field into an array.
In the case it is like evoked above:

$array = explode(',', $row['colors']);

Or whatever appropriate depending on what is exactly the colors field structure.

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

1 Comment

oh I saw references to explode before but I didn't recognize this as the way to handle this situation. Wow, I really appreciate this answer!
0

I used this answer. And it still works perfectly.

If you have an array with only values you should use in_array function as this example:

$array = array("A", "B", "C");
in_array($string, $array) ? "found" : "not found";

If you have an array with keys you should use isset function as this example:

$array = array("A" => "Result A", "B" => "Result B", "C" => "Result C");
isset($array[$string]) ? "found" : "not found";
or like:
if(isset($array[$string])) { do your stuff... }

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.