How to find a value in an array

ggjones
ggjones used Ask the Experts™
on
$haystack = array($vpKey1, $vpKey2, $vpKey3); // NEED TO BE VARIABLES

$vpKey1 = "Water Heaters";

$vpKey2 = "Plumbing Supplies";

$vpKey3 = "Heating";

$needle = "Heating";

if (in_array($needle, $haystack)){ echo '<P>yes';}else { echo '<P>no';}

Open in new window


This always returns "NO". How can this be made to work??

many thanks,

GJ

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:


$vpKey1 = "Water Heaters";

$vpKey2 = "Plumbing Supplies";

$vpKey3 = "Heating";

$haystack = array($vpKey1, $vpKey2, $vpKey3); // NEED TO BE VARIABLES

$needle = "Heating";

if (in_array($needle, $haystack)){ echo '<P>yes';}else { echo '<P>no';}
You need to define the variables BEFORE you attempt to assign them to array values.
Most Valuable Expert 2011
Top Expert 2016
Commented:
If you add error_reporting(E_ALL) to the top of your scripts you will get a Notice when the script accidentally relies on an undefined variable.  However this Notice is off by default (a terrible problem in PHP if you ask me) and so the first line of the code snippet in the question relied on three undefined variables, but you didn't know that because PHP did not tell you.  Run the code snippet to see the effect of the better error_reporting() level.

http://www.laprbass.com/RAY_temp_ggjones.php
Outputs:
no
Notice: Undefined variable: vpKey1 in /home/websitet/public_html/RAY_temp_ggjones.php on line 24
Notice: Undefined variable: vpKey2 in /home/websitet/public_html/RAY_temp_ggjones.php on line 24
Notice: Undefined variable: vpKey3 in /home/websitet/public_html/RAY_temp_ggjones.php on line 24
yes

Summary: put the instructions in the right order to solve this problem, but also give yourself every opportunity to spot and remove errors in the code.

best, ~Ray
<?php // RAY_temp_ggjones.php
echo "<pre>";


// WITH THE DEFAULT ERROR REPORTING -- NO NOTICE OF THE PROBLEM
$haystack = array($vpKey1, $vpKey2, $vpKey3);
$vpKey1 = "Water Heaters";
$vpKey2 = "Plumbing Supplies";
$vpKey3 = "Heating";
$needle = "Heating";
if (in_array($needle, $haystack)){ echo '<P>yes';}else { echo '<P>no';}



// UNSET ALL THE VARIABLES FOR THE SECOND TEST
unset($haystack, $vpKey1, $vpKey2, $vpKey3, $needle);



// WITH THE BETTER ERROR REPORTING
error_reporting(E_ALL);

// THIS ASSIGNMENT WILL THROW A NOTICE
$haystack = array($vpKey1, $vpKey2, $vpKey3);



// UNSET ALL THE VARIABLES FOR THE THIRD TEST
unset($haystack, $vpKey1, $vpKey2, $vpKey3, $needle);



// WITH THE INSTRUCTIONS IN THE RIGHT ORDER -- NO PROBLEM
$vpKey1 = "Water Heaters";
$vpKey2 = "Plumbing Supplies";
$vpKey3 = "Heating";
$needle = "Heating";
$haystack = array($vpKey1, $vpKey2, $vpKey3);
if (in_array($needle, $haystack)){ echo '<P>yes';}else { echo '<P>no';}

Open in new window

Author

Commented:

Thanks for the feedback. You are of course correct... but it didn't solve my larger problem the way I had hoped, as described here:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27239549.html

regards,

GJ

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial