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
}
Related
I want the array length to be retrieved in the controller function, I tried using count($array) . But it just returns 0 independent of the array length. My code,
function daybook()
{
$data['j1'] = $this->lams_master_model->journal_info();
echo count($j1);
}
Somebody please help me on about that..
$j1 is not a assigned variable. You probably mean echo count($data); or echo count($data['j1']);.
Try this
$data['j1'] = $this->lams_master_model->journal_info();
echo count($data['j1']);
count($this->lams_master_model->journal_info());
If you are not certain if lams_master_model->journal_info() return's a array you could allways check the return value within that function or just var_dump($this->lams_master_model->journal_info());
j1 is the name of array not a variable. So use echo count($data['j1']) not echo count($j1);. Alternatively you can also return number of rows by $query->num_rows() from your model instead of using count in controller. For more details you can see : http://ellislab.com/codeigniter/user-guide/database/results.html
you just need to do print_r(count($data)); instead of echo count($j1);.
Have fun.
What is a shortcut to validate if an array exists and contains no values?
for some reason, this looks weird
$warning = array();
if (isset($warning) && empty($warning)) {
//go on...
} else {
//either the array doesn't exist or it exist but contains values...
}
the array needs to exist and must contain no values
That is the shortest you will be able to get it if you do not know whether or not the variable is defined.
If you always go about defining the array ($warning = array()), you could skip the isset step.
First, check if the array object itself is allocated, then the individual indexes for allocation.
if ($warning) {
...
}
Would not that work? Of course before checking this you are probably assigning something to it.
Addendum:
This code outputs no, without even having the array initialized.
if ($warning) echo "yes";
else echo "no";
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";
after submited form i want to check array if array is empty alert error for user. but i get error when submited form:
PHP
$errors = array_filter($_POST['session']);
if (!empty($errors)) {
foreach ($_POST['session'] as $value) {
$session.=$value.',';
}
$session=substr($session, 0 , -1);
}
Warning: array_filter() expects parameter 1 to be array, null given in C:\inetpub\wwwroot\manage\test_bank\index.php on line 729
You need to check wheather it is an array or not, before doing any array operation.
if(is_array($_POST['session'])){
$errors = array_filter($_POST['session']);
}
The warning occurs because array_filters() requires an array to be passed to it. Before passing $_POST['session'] to this function, very if that it is an array:
if(is_array($_POST['session'])) {
$errors = array_filter($_POST['session']);
// continue on
}
use is_arrayfor checking weather it is array or not.
echo is_array($_POST['session']);
This is because $_POST is not an array I guess you are looking for this :
$errors = array_filter($_POST);
The following would most simply check for empty errors or not
!empty($_POST['session'])
Would work provided you are not stuffing empty entries in the $_POST['session'] in no error cases. Why do you need the array_filter?
$_POST is an array, but here $_POST['session'] is not.
You can smply try this:
if(isset($_POST['session']))
{
//do your stuff
}
Change it to array_filter($_POST) because $_POST is an assoc array, or check if $_POST['session'] is an array using this line is_array($_POST['session']) before the array_filter().
You should check first if the variable you are working with is an array before using array functions.
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