php in_array is not working - php

Can you please help me in below code.
In the below code, in_array is not working.
$d = "23232,54454,656565";
$data = explode(",", $d);
$pass = (isset($test['pass'][1]) ? $test['pass'][1] : '');
if(in_array($pass, $data)) {
echo "exist";
} else {
echo "Not Exist";
}
Thanks

i tested your code and put following line to top of that and it work:
$test['pass'][1] = '23232';
$test['pass'][1] is empty and you see "Not Exist" message

Related

How do you make sure an array is empty in PHP?

Im writing a page in HTML/PHP that connects to a Marina Database(boats,owners etc...) that takes a boat name chosen from a drop down list and then displays all the service that boat has had done on it.
here is my relevant code...
if(isset($_POST['form1'])){//if there was input data submitted
$form1 = $_POST['form1'];
$sql1 = 'select Status from ServiceRequest,MarinaSlip where MarinaSlip.SlipID = ServiceRequest.SlipID and BoatName = "'.$form1.'"';
$form1 = null;
$result1 = $conn->query($sql1);
$test = 0;
while ($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$values1[] = array(
'Status' => $row['Status']
);
$test = 1;
}
echo '<p>Service Done:</p><ol>';
if($test = 1){
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
echo '</ol>';
}else{
echo 'No service Done';
}
the issue im having is that some of the descriptions of sevice are simply Open which i do not want displayed as service done, or there is no service completed at all, which throws undefined variable: values1
how would I stop my script from adding Open to the values1 array and display a message that no work has been completed if values1 is empty?
Try this
$arr = array();
if (empty($arr))
{
echo'empty array';
}
We often use empty($array_name) to check whether it is empty or not
<?php
if(!empty($array_name))
{
//not empty
}
else
{
//empty
}
there is also another way we can double sure about is using count() function
if(count($array_name) > 0)
{
//not empty
}
else
{
//empty
}
?>
To make sure an array is empty you can use count() and empty() both. but count() is slightly slower than empty().count() returns the number of element present in an array.
$arr=array();
if(count($arr)==0){
//your code here
}
try this
if(isset($array_name) && !empty($array_name))
{
//not empty
}
You can try this-
if (empty($somelist)) {
// list is empty.
}
I often use empty($arr) to do it.
Try this instead:
if (!$values1) {
echo "No work has been completed";
} else {
//Do staffs here
}
I think what you need is to check if $values1 exists so try using isset() to do that and there is no need to use the $test var:
if(isset($values1))
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
Or try to define $values1 before the while:
$values1 = array();
then check if it's not empty:
if($values1 != '')
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
All you have to do is get the boolean value of
empty($array). It will return false if the array is empty.
You could use empty($varName) for multiple uses.
For more reference : http://php.net/manual/en/function.empty.php

echo statement for false php function

I have created a function as follows:
//function for first name field
function name ($fname) {
//validate to see if field is empty
if (empty($fname)) {
return (false);
}
$welcome_string = '<p> Welcome ' . $fname . '. We\'re glad you\'re here! Take a look around!</p>';
return $welcome_string;
}//end fname function
When the function passes the check I echo the function and get the welcome_string. However, I need to display an error outside of the function when it returns false. I cannot figure out how to do this. Below is the code where I call the function:
echo name($fname);
What you want to do is add a check for that:
$result = name($fname);
if ($result) {
echo $result;
} else {
echo "There was an error.";
}
Using a ternary if, you can do this in shorthand:
$result = name($fname);
echo $result ? $result : "There was an error.";
Or even shorter without introducing a new variable:
echo name($fname) ?: "There was an error.";
You could simply do:
$result = name($fname);
echo $result?$result:"error message";
<?php
function greeting($name)
{
if (empty($name))
return false;
$welcome_string = 'Welcome ' . $name . ". We're glad you're here! Take a look around!\n";
return $welcome_string;
}
foreach(['Rita', 'Sue', '', null, 0, '0'] as $name)
echo ($message = greeting($name))
? $message
: "Who are you?\n";
Output:
Welcome Rita. We're glad you're here! Take a look around!
Welcome Sue. We're glad you're here! Take a look around!
Who are you?
Who are you?
Who are you?
Who are you?

Comparing array to string PHP

I want to check if the data is match between array and the string.
Im trying to check if the string is equals to each other but the problem is the condition is returning false and both value of $subex2[1] and $subdat is IT100. I think the problem is you can't compare an array to a string. Can someone help me about this?
here's the code
$subex = $objPHPExcel->getActiveSheet()->getCell('D8')->getValue();
$subex2 = explode(":", $subex);
$q = "select * from class where id='$id'";
$r = mysql_query($q);
$data = mysql_fetch_array($r);
$subdat = $data['subject'];
if($subdat == $subex2[1]) {
echo "Data matched";
}else {
echo "Data doesn't matched";
}
As mentioned in the comments. One value has an space before it. You can solve this kind of problems like this:
if(trim($subdat) == trim($subex2[1])) {
echo "Data matched";
} else {
echo "Data doesn't matched";
}
for case sensitive issue, this trick should apply.
if(strtolower(trim($subdat)) == strtolower(trim($subex2[1]))) {
echo "Data matched";
} else {
echo "Data doesn't matched";
}
this test works fine
$subdat = "IT100";
$subex2[1] = "IT100";
if($subdat == $subex2[1]) {
echo "Data matched";
}
You are not comparing same string
You first use array_map and trim values, delete space, next use in_array, example
$subex2 = array_map('trim',$subex2);
if( is_array($subex2) ){
if(in_array($subdata, $subex2)){
echo "Data matched";
} else {
echo "Data doesn't matched";
}
}
It's always good to check if it's actually an array, with is_array
Reference in_array https://www.w3schools.com/php/func_array_in_array.asp
try this:
$isMatched = strval($subex2[1]) === strval($subdat) ?: false;

return and echo my array from a function

I have a function that returns either a value into a variable if it is successful or it returns an errors array. see part of it below.
function uploadEmploymentDoc($var, $var2){
$ERROR = array();
if(empty($_FILES['file']['tmp_name'])){
$ERROR[] = "You must upload a file!";
}
//find the extensions
$doctypeq = mysql_query("SELECT * FROM `DocType` WHERE `DocMimeType` = '$fileType'");
$doctype = mysql_fetch_array($doctypeq);
$docnum = mysql_num_rows($doctypeq);
if($docnum == 0){
$ERROR[] = "Unsupported file type";
}
if(empty($ERROR)){
// run my code
return $var;
} else{
return $ERROR;
}
then when I run my code
$result = uploadEmploymentDoc(1, 2);
if($result !=array()){
// run code
} else {
foreach($result as $er){
echo $er."<br>";
}
}
Now my question is this. Why is my function running my code and not showing me an error when I upload an unsupported document type. Am I defining my foreach loop correctly? For some reason I cant get my errors back.
you should write like this-
if(is_array($result)){
foreach($result as $er){
echo $er."<br>";
}
} else {
//your code for handling error
}
You can get more info :http://us2.php.net/is_array
Try use
$result = uploadEmploymentDoc(1, 2);
if(!is_array($result)){
// run code
} else {
foreach($result as $er){
echo $er."<br>";
}
}
Probably will be better add parameter by reference to function for the errors array. From function return "false" if error and value if no error occurred.

in_array not working explode

I am not able to figure out why my condition is not working while the ip address is in the array. Why condition is failing as shown in image
<?php $valid_ip_list = explode(',',$this->valid_ips);
echo $client_ip = $_SERVER['REMOTE_ADDR'];
print('<pre>');
print_r($valid_ip_list);
if(in_array($client_ip ,$valid_ip_list))
{
echo 'I am here';
}
else
{
echo 'Condition fail';
}
?>
Problem solved with the help of array_map('trim', explode(',', $valid_ips))
This should help
$valid_ips = '192.100.100.61,192.100.100.2,127.0.0.1';
// authorized
if (in_array($_SERVER['REMOTE_ADDR'], array_map("trim", explode(',', $valid_ips)))) {
//...
}
// unauthorized
else {
//...
}

Categories