Deep array !empty check in php - 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
}

Related

check the variable have no count

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.

Checking if array is empty or not doesn't seems to work

Simple array and simple check if is array or object .. yet page crashing when there is no array data instead of showing No. This is the array
$url=get_curl_content_tx("https://example.com");
$arr = json_decode($url, true);
if (is_array($arr['outputs']) || is_object($arr['outputs'])) {
echo 'Yes';
}
else {
echo 'No';
}
if I receive fail i.e. no data from the url and $arr['outputs'] is empty I've got blank page with
Undefined index: outputs
instead of No. Doesn't if (is_array($arr['outputs']) || is_object($arr['outputs'])) check if is array or no?
If there is data in $arr['outputs'] everything is fine.
You need to use isset or array_key_exists to check the key exists in the $arr array before referring to it.
if (isset($arr['outputs']) && is_array($arr['outputs'])) {
You want to access a non-existent array, which gives you an error, no matter what function you are using right before. To solve this, check first if the array exists with isset():
if(isset($arr)) {
// Just gets executed if the array exists ans isn't nulll
} else {
// Array is null or non-existend
}
and add then your code in the if-else.

how to enter '0' with checking the key is not empty?

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
}

shortcut to validate if an array exists and contains no values

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";

php check empty array

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.

Categories