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.
Related
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.
When i submit my form and if the second parameter is empty.
It throws error.
Message: in_array() expects parameter 2 to be array, null given
Filename: user/Users_groups.php Line Number: 250
I know there is no second parameter. $this->session->userdata('modify')
if (!in_array('user/users_groups', $this->session->userdata('modify'))) {
$this->error['warning'] = 'You do not have permission to modify';
}
Is there away to make it stop throwing that error when user or I try to submit form if no second parameter $this->session->userdata('modify')
But still have that code.
Try to check first if parameters are valid:
<?php
if(is_array($this->session->userdata('modify')) && !empty($this->session->userdata('modify')))
{
if (!in_array('user/users_groups', $this->session->userdata('modify')))
{
$this->error['warning'] = 'You do not have permission to modify';
}
}else{/* handle errors */}
?>
Replace the null value with an empty array before the if statement:
Could use a ternary operator like this:
$array = (is_array($this->session->userdata('modify')))
? $this->session->userdata('modify')
: Array()
It will use the 'modify' array if it's an array, else will pass an empty array
I would also check array_search() function because it can be compared with NULL wich is returned if irregular parameters set.
5.3.0 As with all internal PHP functions as of 5.3.0, array_search() returns NULL if invalid parameters are passed to it.
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 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
}
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
}