In laravel I have send the ajax response to the controller. there I have check the condition to check the count of the response geo as if(count($request['geo'])) and for some reason I want to check the condition if no count for geo as if(count($request['geo']) == 0).but its not working.
How to check the condition 'if no count for geo' in laravel
if $request['geo'] contains an array, then count($request['geo']) == 0 will be true for empty array [].
But if $request['geo'] contains a string (e.g. '[]') - it won't work and you can't use count() for strings. For checking empty strings you can use $request['geo'] == '' or !$request['geo']
Probably you need to check the type of value first, e.g. using is_array($request['geo'])
P.S. !$request['geo'] works for both - empty arrays and empty strings
You can check like this
if(empty($request['geo'])){
// your code
}
You can use php function gettype and count.
So you can use following code:
if(gettype($request['geo']) == 'array') {
if (count($request['geo']) == 0) {
// your code here
} else { // count > 0
// your other code here
}
}
Hope to helpful.
Related
Is there a better/shorter way to perform the following:
$stock_array = array("soccer" => "0");
if (count($stock_array) == 1 && in_array('0', $stock_array)) {...}
I want to check if the array has 1 element, and the value is 0.
Thanks in advance
Based on your title, this provides if the count is one and its value is 0
$count = array_count_values($stock_array);
if ($count == array('0' => 1)) {
//something
}
Or you can also use array_filter() with empty(), if you are looking for a pre-function kind of answer: This will return True if all the values in the array are empty or 0.
if (empty(array_filter($stock_array))) {
//something
}
Yes, you can try using the following code.
if (array_search('0', array_values($stock_array)) == 0) {...}
But this doesn't tell if the array has only this element.
From your question, I can understand you are trying to do:
Check the length of array to be 1.
Check if the value exists in the array.
The solution I provided, checks if that's the first value of the array. But to check the length of array, you need to have another as well.
let me explain my problem..
This is my array
$data['id']=1;
$data['name']='Bhavik';
$data['salary']=0;
$data['TA']=0;
before processing on the data I am checking whether the key is empty or not if it is empty then we have to neglect it.
like..
if(!empty($data['id']))
{
$responseData['id']=$data['id'];
}
if(!empty($data['name']))
{
$responseData['name']=$data['name'];
}
if(!empty($data['salary']))
{
$responseData['salary']=$data['salary'];
}
if(!empty($data['TA']))
{
$responseData['TA']=$data['TA'];
}
print_r(responseData);
it gives me only id and name
it considers 0 as empty
what if I want salary and TA with Zero value?
I can do it with checking whether the key is exists or not and the check whether it has any value or not but is there any other way.
I am not using key_exists just because if we pass array like $data['id] without any value still that condition will be true but it gives error bcoz it does not have any value while processing the data.
so any help will be appreciated.
Thank you
you can add second OR condition within IF condition statement like
if(!empty($a) || $a==0)
Try this hope it will solve your problem
You can use this function instead of empty:
function is_blank($value) {
return empty($value) && !is_numeric($value);
}
Treating the values as strings, check the string length with strlen() and the validity of characters inside it, in this case (from the description I assume all-digit values) - ctype_digit().
If you don't know whether the key exists, check that with isset().
You can check using strict compare
if(null!==$a){
//... 0 is passed as true value
}
How to use PHP to check form field for specific value?
The value '7' would need to be inserted or else the form field will be invalid; so invalid with all other numbers then 7.
if(empty($_POST['captcha']))
{
$this->add_error("Did you solve the captcha?");
$ret = false;
}
Works to check if any value -- but I need to specify, must be 7.
if(strlen($_POST['captcha']) =!7) // tried this but form completely died or disappeared
{
$this->add_error("Did you solve the captcha?");
$ret = false;
}
There are two things to note here:
You shouldn't use empty() here, because "0" is considered empty as well and it seems like it could be the answer of an equation; use isset() instead to just check whether the value is part of the request.
You're trying to check the length of $_POST['captcha'] instead of comparing its value.
So:
if (!isset($_POST['captcha']) || $_POST['captcha'] != 7) {
$this->add_error('Did you solve the captcha?');
$ret = false;
}
You have two problems:
=! should be !=. See comparison operators.
if(strlen($_POST['captcha']) != 7)
You also need to turn on error reporting.
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
I need to check array value, but when array is empty, I get this: Error: Cannot use string offset as an array
if (!empty($items[$i]['tickets']['ticket'][0]['price']['eur'])) { //do something }
How to do it correctly?
You need to check if the variable is set, then if it is an array and then check if the array's element is set. The statements of the if will be executed in order and will break when one is false.
if(isset($items) && is_array($items) && isset($items[$i]['tickets']['ticket'][0]['price']['eur'])) {
//jep it's there
}
Or just try it (extra sipmle variant):
if (!isset($items[$i]['tickets']['ticket'][0]['price']['eur'])) {
// do action
}