This is my code:
if(is_array($ItemAttr["Actor"])){
$Actors = implode(", ", $ItemAttr["Actor"]);
} else {
$Actors = $ItemAttr["Actor"];
}
I'm getting undefined index: Actor in **line 1** and **line 3**
I guess i should use isset() function. Can anyone tell me how to combine that function with is_array() function..?
Not sure if this is what you mean but:
if( isset($ItemAttr["Actor"]) && is_array($ItemAttr["Actor"])){
....
}
In this case you are checking if the index exist before accessing its value.
Related
I am trying to write a function to avoid getting variable undefined error. Right now, i have a code like this:
function check($str){
if(isset($str)){
$s = $str;
} else {
$s = "";
}
}
check($_GET['var']);
The get var is not set. I am getting a variable undefined error on my screen. How do i alter my function to not throw this error and just return "" if it is not set? I don't want to have to code 100 if statements to avoid getting variable undefined. Thanks.
We already have in PHP a construct to check that. It is called isset(). With it you can check whether a variable exists. If you would like to create it with some default values if it doesn't exist yet, we also have syntax for it. It's null-coalescing operator.
$_GET['var'] = $_GET['var'] ?? '';
// or since PHP 7.4
$_GET['var'] ??= '';
Although I'm not sure if it is the right way of doing it, for the sake of providing an answer you can pass the variable by reference, this allows you to get away with passing undefined variables and check if it is set inside the function..
function check(&$str){
if(!isset($str)){
$str = "not set";
}
}
check($_GET['var']);
echo $_GET['var'];
I am struggling with the undefined notice comes and sometimes not. I am not sure why I am having this error.
foreach($_POST['toplevel_menus'] as $toplevel_menu){
$toplevel_extracted = explode("|", $toplevel_menu);
$submenu_id = $toplevel_extracted[5];
if(isset($_POST[$submenu_id]) && !empty($_POST[$submenu_id])){
foreach($_POST[$submenu_id] as $submenu){
$extracted = explode("|" $submenu);
$submenu_name = (isset($_POST[$subname][$extracted[1]]))
? trim($_POST[$subname][$extracted[1]])
: "";
}
}
}
The line number 7 is
$submenu_name = (isset($_POST[$subname][$extracted[1]])) ? trim($_POST[$subname][$extracted[1]]) : "";
The best practice to always access arrays via index is to check them with isset first. This way you can make sure to avoid such errors.
Looking to get a count of particular key=>values in an multi dimensional array. What I have works i.e. the result is correct, but I can't seem to get rid of the Undefined Index notice.
$total_arr = array();
foreach($data['user'] as $ar) {
$total_arr[$ar['city']]++;
}
print_r($total_arr);
Any ideas? I have tried isset within the foreach loop, but no joy...
$total_arr = array();
foreach($data['user'] as $ar) {
if(array_key_exists($ar['city'],$total_arr) {
$total_arr[$ar['city']]++;
} else {
$total_arr[$ar['city']] = 1; // Or 0 if you would like to start from 0
}
}
print_r($total_arr);
PHP will throw that notice if your index hasn't been initialized before being manipulated. Either use the # symbol to suppress the notice or use isset() in conjunction with a block that will initialize the index value for you.
Seems an easy one, but cannot work out why:
if(!isset($_SESSION[$_REQUEST["form_id"]]))
{
//do stuff
}
reutrns
Notice: Undefined index: form_id
empty returns same response.
This has been driving me mad for a while. :)
You're calling isset for $_SESSION but as the error states the issue is with $_REQUEST['form_id'] not being set.
if (!isset($_REQUEST['form_id']) || !isset($_SESSION[$_REQUEST['form_id']])) {
That's because it resolves $_REQUEST['form_id'] first and that causes the notice. You could do this instead:
if (!isset($_REQUEST['form_id']) || !isset($_SESSION[$_REQUEST["form_id"]]))
{
//do stuff
}
please check if key exists with
array_key_exists('form_id', $_REQUEST);
before checking value with
isset($_REQUEST['form_id']);
or check if your params are empty like
<?php
if (!empty($_REQUEST['form_id'])) {
// do anything
}
else
{
// I can't find the key in array
}
?>
There is post of select form like:
<select name="option[color][0]">
<select name="option[color][1]">
// option[color][2] isnt posted
Some products doesnt have that select, and then when I try to get them from post, each time if select isn't posted, im getting error like:
Undefined offset: 2
How to check if something is posted?
Tried:
$ids = $_POST['id'];
$option = $_POST['option'];
foreach ($ids as $key => $id)
{
//Undefined offset: 2
if( $option['color'][$key] )
{
$_SESSION[$key]['option']['color'] = $option['color'][$key];
}
//Undefined offset: 2
if( !empty($option['color'][$key]) )
{
$_SESSION[$key]['option']['color'] = $option['color'][$key];
}
//Undefined offset: 2
if( isset($option['color'][$key]) )
{
$_SESSION[$key]['option']['color'] = $option['color'][$key];
}
//... etc
}
Etc.... what ever I try, there is error :(
Please help
Try array_key_exists to see if it exists.
isset($option['color'][$key]) is the way to go.
Check the exact line of code the error occurs when you still get it using isset().
if it is always 0,1,2 or any line of consecutive integers you could do if(count($option['color']) > $key ){}
if( isset($option['color'][$key]) )
{
$_SESSION[$key]['option']['color'] = $option['color'][$key];
}
use isset or empty.
http://php.net/manual/en/function.isset.php
http://us2.php.net/manual/en/function.empty.php
for example:
if (isset($array['idx'])){ ... }
if (!empty($array['idx'])){ ... }