Undefined offset: 7 in /home/test/index.php - php

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.

Related

These errors are not resolving even there's no mistake it is not perfoming reorder

Warning: array_key_exists() expects parameter 2 to be array, string given in D:\xampp\htdocs\ecom\customer\data.php on line 45
Notice: Array to string conversion in D:\xampp\htdocs\ecom\customer\data.php on line 51
My Code
$qry = dbQuery("SELECT `orders_product`.`pd_id`, `op_quantity` FROM `orders_product`
INNER JOIN `products_detail` ON `products_detail`.`pd_id`=`orders_product`.`pd_id`
WHERE orders_product.order_id = $id");
if(dbNumRows($qry)>0) {
$cart="";
if(!isset($_SESSION['cart'])) { $_SESSION['cart']="";}
$result = dbFetchArray($qry);
while($result = dbFetchArray($qry)) {
$product_detail_id = $result["pd_id"];
$order_quantity = $result["op_quantity"];
// echo $product_detail_id;
#print_r($result);
if(array_key_exists($product_detail_id, $_SESSION['cart'])){
$quantity = $_SESSION['cart'][$product_detail_id]['quantity'] + $order_quantity;
$_SESSION['cart'][$product_detail_id]['quantity'] = $quantity;
} else {
$cart = array("pd_id"=>$product_detail_id, "quantity"=> $order_quantity);
$_SESSION['cart'][$product_detail_id] = $cart;
print_r($cart);
}
}
}
$return = 1;
echo json_encode($return);
}
First problem
Warning: array_key_exists() expects parameter 2 to be array, string given in D:\xampp\htdocs\ecom\customer\data.php on line 45
That's because you use $_SESSION['cart'] with function array_key_exists. So when a person enters the website for the first time the variable $_SESSION['cart'] is not set.
What you do then is assign an empty string.
if(!isset($_SESSION['cart'])) { $_SESSION['cart']="";}
So then you use empty string with function array_key_exists.
Solution
To resolve this issue simply change
if(!isset($_SESSION['cart'])) { $_SESSION['cart']="";}
to
if(!isset($_SESSION['cart'])) { $_SESSION['cart']=array();}
Second problem
Notice: Array to string conversion in D:\xampp\htdocs\ecom\customer\data.php on line 51
Now, this is only a notice so that's not a big issue. It may not even be something wrong. After you change to not display errors it won't affect your website in any way.
But why is this showing? Again the same reason as with the previous problem. You assign empty string to variable $_SESSION['cart'] but then you treat is as an array
$_SESSION['cart'][$product_detail_id] = $cart;
When you change the assignment from empty string to empty array this issue will be solved as well.
By the way when you write a one line if statement a good practice is not to use the braces
So I would write it like
$_SESSION['cart'] = isset($_SESSION['cart']) ? $_SESSION['cart'] : [];
Or even better
$_SESSION['cart'] = $_SESSION['cart'] ?? [];
But this is only php 7.0 and higher
I hope you understand now what was wrong and why. If you have any questions feel free to ask. I'll try to reply the best away I can.

I'm trying to continue an undefined offset code in php

I have code in php. The values of variables are empty but I want to continue if the variable is empty or not.
$str_array = explode(",",ltrim($e4view, ", "));
if (isset($str_array[0][1][2])) {
$f4irst = $str_array[0];
$s4econd = $str_array[1];
$t4hird = $str_array[2];
echo $f4irst + $s4econd + $t4hird;
Do you want to do this?
if (empty($str_array[0]) && empty($str_array[1] …) {

explode() offset and delimiter warnings when delimiter is valid

I'm having a heck of a time trying to figure out what is going on. I have a block of code that is throwing some errors. Outside of the Warnings and Notices the code works fine and the arrays are not empty. But why I am getting those warnings and notices is bothersome. In my test environment I don't get the errors however when put on a different server they pop right up. I am stumped as to why. The lines of code that are throwing the errors and the errors being thrown are as follows
$libTitle = explode($libYearRes[0], $value);
Notice: Undefined offset: 0
Warning: explode(): Empty delimiter
$sortLibYearRes = explode(' ', $libYearRes[0]);
Notice: Undefined offset: 0
$libMovieRes = $sortLibYearRes[1];
Notice: Undefined offset: 1
The entire block of code is as follows with the problem lines denoted with a # in them
function checkMovieLibrary($movieTitle,$movieLibrary){
foreach ($movieLibrary as $value) {
preg_match('/\(\d\d\d\d\)\s\d\d\d*[Pp]/', $value, $libYearRes);
$libTitle = #explode($libYearRes[0], $value);
$libMovieTitle = rtrim($libTitle[0]);
$sortLibYearRes = #explode(' ', $libYearRes[0]);
//list($libMovieYear,$libMovieRes) = explode(' ', $libYearRes[0], 2);
$libMovieYear = $sortLibYearRes[0];
$libMovieRes = #$sortLibYearRes[1];
preg_match('/\(\d\d\d\d\)\s\d\d\d*[Pp]/', $movieTitle, $newfileYearRes);
$newFileTitle_array = explode($newfileYearRes[0], $movieTitle);
$newFileTitle = rtrim($newFileTitle_array[0]);
$sortFileYearRes = explode(' ', $newfileYearRes[0]);
$newFileMovieYear = $sortFileYearRes[0];
$newFileMovieRes = $sortFileYearRes[1];
if(stripos($libMovieTitle.' '.$libMovieYear, $newFileTitle.' '.$newFileMovieYear) !== false){
$moviesList_array[] = array('OriginalFile' => $value, 'NewFile' => $movieTitle);
return($moviesList_array);
}
}
return false;
}
//$movieTitle = "10 Cloverfield Lane (2016) 1080p.mkv"; //This is an example of $movieTitle string
//$movieLibrary = array('10 Cloverfield Lane (2016) 1080p.mkv','The Goonies (1985) 1080p.mp4'); this is an example of the $movieLibrary array
$doesMovieExist = checkMovieLibrary($Movie,$movieLibrary);
if(is_array($doesMovieExist) && $doesMovieExist !== false){
foreach($doesMovieExist as $fileKey => $fileValue) {
$originalFile = $fileValue['OriginalFile'];
$newFile = $fileValue['NewFile'];
}
//do some stuff here
}
First of all dont use # because it will bypass the error message. So user error_reporting(E_ALL) till development phase.
Then you are getting undefined offset it means your array is empty of does not exist so put a condition to check if it exist then the explode should work. It must be like that :
if(!empty($libYearRes[0]) || isset($libYearRes[0])) {
$libTitle = #explode($libYearRes[0], $value);
}
Undefined offset error means you're referring an array key that does not exist. If you like to see the array before processing it, use variable dump (var_dump) then you will see these array keys are not available for the given scenario.
you can avoid this error by checking array key really exist or not by using isset function. as example
if(isset($libYearRes[0])){ // will be true only if $libYearRes[0] is exist
$libTitle = explode($libYearRes[0], $value);
}

Notice: Undefined index with used isset and $_GET

this is my code
if(isset($_GET['delt']) || isset($_GET['editt'])){
$delt = intval($_GET['delt']);
$editt = intval($_GET['editt']);
}
when run the code I get the error
Notice: Undefined index: delt in
/opt/lampp/htdocs/tel-s/admin/top_a.php on line 116
I'm confused because i use isset and test used empty but do get the same problem
Your are testing only if one of params is set and later use both of them no matter if it's actually set.
Use && (and) instead of || (or):
if (isset($_GET['delta']) && isset($_GET['editt']))
OR
if (isset($_GET['delta'], $_GET['editt']))
Check both of variables for exists or change code
if( isset($_GET['delt']) ) {
$delt = intval($_GET['delt']);
}
if ( isset($_GET['editt']) ) {
$editt= intval($_GET['editt']);
}
It must have gotten $_GET['editt'] but couldn't get $_GET['delt'] and threw an error because it couldn't set anything to $delt. Consider #Justinas' answer.
did your url contains both "delt" and "editt" parameters ?
what is the out put of print_r($_GET) ?
isset function checks whether a variable is set or not ?
if you pass a string to intval() it will return 0

php undefined index..using isset() with is_array()

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.

Categories