match a variable with at least one value in an array - php - php

I have one variable that comes from a database.
I then want to check whether that value is the same of one of the values in an array.
If the variable matches one of the array values, then I want to print nothing, and if the variable does not match one of the array values, then I want to print something.
This is the code I have been trying without luck, I know that contains is not valid code, but that is the bit I cannot find any info for:
<?php
$site = getStuff();
$codes = array('value2', 'value4');
if ($codes contains $site)
{
echo "";
}
else
{
echo "something";
?>
So if the database would return value1 for $site, then the code should print "something" because value1 is not in the array.

The function you are looking for is in_array.
if(in_array($site, array('value2', 'value4')))

if(!in_array($site,$codes)) {
echo "something";
}

To provide another use way to do what the other answers suggest you can use a ternary if
echo in_array($site, $codes)?"":"something";

Related

PHP fastest way to iterate over list? [duplicate]

Okay, so I have a few possible matches that I need to test. It can either equal A or B, so the obvious way to test it would be something like this:
if($val=="A"||$val="B"){
echo "yup";
}
I was just wondering if their were an easier way to test values without restating the variable for every value, like this (I know this doesn't work):
if($val==("A"||"B")){
echo "yuppers";
}
Is something like this possible?
You can use in_array
$array = array('A','B','other values');
if(in_array($val, $array)){
// value is in array
}else {
// invalid value
}
You can use in_array:
if (in_array($val, array('A', 'B'))) {
echo 'yuppest';
}
you can add "A" and "B" to the array and use in_array method but this is definitely not more efficient than $val=="A" || $val =="B"

Variable variable with arrays

I'm confused in how to use $$ to use a string as a variable, mainly when it comes to use a string to refer an array index.
Consider the following case:
$colors = array(
'r'=>"red",
'b'=>"blue"
);
$vr = "colors[r]"; //I tried even this "color['r']"
echo $$vr; // I tried even this ${$vr}
Can anyone tell if it is possible to do the above.
expected o/p is red using "color[r]" as string and then using it as variable.
You can't do that directly. Consider the following:
$varName = array_shift(explode('[', $vr));
foreach($$varName as $key=>$value){
echo $key.": ".$value."<br />";
}
this will print out:
r: red
b: blue
The variable variable is just the first part (colors). You can't include the key in this.

Check to see if a variable exists in an array

I have an array and I am trying to see if it contains a certain value that is represented by a variable. The value will always be numeric The array is created from a MySQL select query
Variable:
$_SESSION['id']
Array
$likes_row
A solution is to use the in_array() function: http://php.net/manual/en/function.in-array.php
if(in_array($_SESSION['id'], $likes_row))
{
//Array contains the value
}
if(in_array($_SESSION['id'], $likes_row)){
echo "we have likes!";
}
You can try this:
if(in_array($_SESSION['id'], $likes_row, TRUE))
{
// found it, now do something
}

PHP array element comparison

Learning PHP and I have a question.
How does one obtain an element from an array and determine if it is equal to a static value? I have a return set from a query statement (confirmed the array has all values).
I tried:
<? if($row["rowValue"] == 1) {
}
?>
I was expecting the value to be 1, but it's returning null (as if I'm doing it wrong).
You're pretty much there; something like this should confirm it for you:
echo "<p>Q: Does ".$row["rowValue"]." = 1?</p>";
if($row["rowValue"] == 1) {
echo "<p>A: Yes ".$row["rowValue"]." does equal 1</p>";
} else {
echo "<p>A: No, '".$row["rowValue"]."' does not equal 1</p>";
}
If that's still returning 'No' you could try viewing the whole of the $row array by doing a var dump of the array like so:
var_dump($row);
This will give you detailed output of how the array is built and you should be able to see if you are calling the correct element within the array.
What is returning null?
Try this:
if($row["rowValue"] === 1) { ... }
Make sure there is an element in $row called rowValue.
maybe try:
<? if($row[0]["theNameOfAColumn"] == 1) {
}
?>
Usually databases return rows like row[0], row[1], row[2], etc.
I am not sure what exactly you are doing, but try using array_filp() which will Exchanges all keys with their associated values
than you can do like
if($row["rowValue"] == 1) {
http://in1.php.net/manual/en/function.array-flip.php
If you're pulling it from mysqli_fetch_row then it wants a number, not a column name. If it's being pulled from mysqli_fetch_array then it will accept a column name.
http://php.net/manual/en/mysqli-result.fetch-row.php
http://php.net/manual/en/mysqli-result.fetch-array.php

"Large multiple variable echo's" way to make simpler?

Say I am echoing a large amount of variables in PHP and I wont to make it simple how do i do this? Currently my code is as follows but it is very tedious work to write out all the different variable names.
echo $variable1;
echo $variable2;
echo $variable3;
echo $variable4;
echo $variable5;
You will notice the variable name is the same except for an incrementing number at the end. How would I write a script that prints echo $variable; so many times and adds an incrementing number at the end to save me writing out multiple variable names and just paste one script multiple times.?
Thanks, Stanni
You could use Variable variables:
for($x = 1; $x <= 5; $x++) {
print ${"variable".$x};
}
However, whatever it is you're doing there is almost certainly a better way: probably using Arrays.
I second Paolo Bergantino. If you can use an array that would be better. If you don't how to do that, here you go:
Instead of making variables like:
$var1='foo';
$var2='bar';
$var3='awesome';
... etc... you can make a singe variable called an array like this:
$my_array = array('foo','bar','awesome');
Just so you know, in an array, the first element is the 0th element (not the 1st). So, in order to echo out 'foo' and 'bar' you could do:
echo $my_array[0]; // echoes 'foo'
echo $my_array[1]; // echoes 'bar'
But, the real benefits of putting value in an array instead of a bunch of variables is that you can loop over the array like this:
foreach($my_array as $item) {
echo $item;
}
And that's it. So, no matter how many items you have in your array it will only take those three lines to print them out. Good luck you with learning PHP!
Use dynamic variable names:
for($i=1; $i < 6; $i++) echo ${'variable'.$i}

Categories