Trying to match variables in a PHP array
Posted
by
Nick B
on Stack Overflow
See other posts from Stack Overflow
or by Nick B
Published on 2012-10-06T09:29:40Z
Indexed on
2012/10/06
9:37 UTC
Read the original article
Hit count: 226
I'm stuck with a php array problem. I've to a webpage that takes values from a URL, and I need to cross reference those values against some values on the page and if they match output a 'yes'. It's an expression engine bodge job.
The URL is something like domain.com/page/C12&C14
The C12 and C14 represent different categories. I've taken the last bit of the url, removed the 'C' from the values and then exploded the 12&14 into an array.
I print_r the array on the page and it shows: Array ( [0] => 12 [1] => 14 )
So, the values are in the array. Lovely.
Now on the page I have an html list which looks like
10
12
14
15
I want to output a YES next to the variables that are current in the array so the ideal output would be:
10
12 - YES
14 - YES
15
I was trying this but it keeps just saying No next to all of them.
$currentnumber = 12;
foreach ($tharray as $element) {
if ($element == $currentnumber) { echo "Yes"; } else { echo "No"; }
}
I thought that should work, but it's not. I checked and the array and the variable are both stings. I did a strlen() on both to see if they are the same, but $currentnumber outputs '13' and the array variable outputs '2'.
Any ideas as to why it's saying 13?
Is the variable the wrong type of string - and if so how would I convert it?
© Stack Overflow or respective owner