How to catch this error: Notice: Undefined offset: 0
You need to define your custom error handler like:
<?php
set_error_handler('exceptions_error_handler');
function exceptions_error_handler($severity, $message, $filename, $lineno) {
if (error_reporting() == 0) {
return;
}
if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
}
$a[1] = 'jfksjfks';
try {
$b = $a[0];
} catch (Exception $e) {
echo "jsdlkjflsjfkjl";
}
Notice: Undefined offset: 0 foreach
This is what you got :
print_r($result_arr);
This give you this array :
Array (
[Items] =>
Array (
[0] => Array (
[SKU] => 123
[Quantity] => 13
[ProductName] => tet prod
[Description] => blah blah ...
And this is what you do :
foreach ($result_arr[0]["http_response_body"]["Items"] as $key => $value) {
You get Notice: Undefined offset: 0
just because as you can see there is no $result_arr[0]
in your array.
So just do :
foreach ($result_arr["Items"] as $key => $value) {
And it should works !
Hope it helps
What is causing Undefined offset: 0 php
This line causes an error:
$result->results[0]
You need to check if $result->results
has 0 key.
UPDATE:
To troubleshoot your problem you need to catch response that you do not expect:
$response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");
$result = json_decode($response);
try {
$lat[] = trim($result->results[0]->geometry->location->lat);
$lng[] = trim($result->results[0]->geometry->location->lng);
} catch (\Exeption $e) {
var_dump($result);
}
When an exception occurs, you will see the response that causes your problem, and then you need to add a response format check based on what you received.
For example if you receive something like this:
{"results":[], "status": "ZERO_RESULTS"}
Your code should looks like this:
$response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");
$result = json_decode($response);
if(!empty($result->results)){
$lat[] = trim($result->results[0]->geometry->location->lat);
$lng[] = trim($result->results[0]->geometry->location->lng);
}
See also https://developers.google.com/maps/documentation/geocoding/intro
undefined offset PHP error
If preg_match
did not find a match, $matches
is an empty array. So you should check if preg_match
found an match before accessing $matches[0]
, for example:
function get_match($regex,$content)
{
if (preg_match($regex,$content,$matches)) {
return $matches[0];
} else {
return null;
}
}
Undefined offset: 0 - PHP Errors in Ajax Response
You need to properly initiate $counts
as:
$counts = array(0,0);
because you are currently trying to increment a non-existent position:
// This doesn't work when position zero doesn't exist.
$counts[0] += $cart_item['quantity'];
Same thing applies to your $counts[1] +=
line.
Problems with arrays: Notice: Undefined offset: 0
Check Isset in your error line.
The isset () function is used to check whether a variable is set or not The isset() function return false if testing variable contains a NULL value.
for($i = 0; $i < count($aDataTableDetailHTML); $i++)
{
for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
{
if(isset($aDataTableDetailHTML[$i][$j])){
$aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
}
}
}
Related Topics
Search for PHP Array Element Containing String
Laravel: Connect to Databases Dynamically
Simplest Way to Increment a Date in PHP
PHP File Listing Multiple File Extensions
Can't Send Email to Addresses at My Own Domain
Storing Form Data as a Session Variable
How to Use Open Id as Login System
How to Increment Letters Like Numbers in PHP
Upload 1Gb Files Using Chunking in PHP
Laravel App Stopped Working After Upgrading to PHP 8
Get the Query Executed in Laravel 3/4
Differencebetween Find(), Findorfail(), First(), Firstorfail(), Get(), List(), Toarray()