• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

undefined offset php

PHP Notice:  Undefined offset: 10 in file.php on line 112
PHP Notice:  Undefined offset: 11 in file.php on line 112

                    foreach ($traceroute as $key => $result) {
                    $ip = $result['ip'];
                    $datetime = $result['datetime'];
                    $hop = $result['hop'];
                if(isset($arrayip[$ip])) {
                    if ($ip == $array[$hop]['ip']) {                           < This is line 112
                        $datetime = $array[$hop]['datetime'];
                        }
                }


Guessing this can't be fixed without having to show all of my code... which I can't do.
0
projects
Asked:
projects
1 Solution
 
GaryCommented:
Well it's telling you there is no such index as 10 in the array
0
 
projectsAuthor Commented:
Ok but how do I fix this problem without having to call a programmer :)
0
 
Ahmed MerghaniCommented:
Replace you code with this:
foreach ($traceroute as $key => $result) {
                    $ip = $result['ip'];
                    $datetime = $result['datetime'];
                    $hop = $result['hop'];
                if(isset($arrayip[$ip])) {
                    if ($ip == @$array[$hop]['ip']) {                           < This is line 112
                        $datetime = $array[$hop]['datetime'];
                        }
                }

Open in new window

I just add "@" before the array.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
GaryCommented:
if(isset($arrayip[$ip]) && isset($array[$hop]['ip'])) {
            if ($ip == $array[$hop]['ip']) {
                $datetime = $array[$hop]['datetime'];
    }
}

Open in new window

0
 
Ray PaseurCommented:
Undefined offset is closely related to undefined index. In both cases you're telling PHP to find information that does not exist.  In the case of undefined offset the array is numerically indexed and there is no position with the numerical index that you've requested.  With undefined index you're using a named position in an associative array, and the array index name does not exist.

How / whether you choose to fix this is going to be dependent on what you want for output.  Without seeing the data and understanding the larger context it's not really possible to give you a "good" answer, but we might get lucky.  It appears to be an issue of data inconsistency -- the programming expects data that is somehow missing.  Here are some of the things only you can know about this application.

1. Is it OK to ignore this Notice, and just skip over the missing data?
2. Is it necessary to trap the condition that is indicated by the missing data?  
3. Perhaps there is another programmatic or manual process that is needed to "scrub" the data?  
4. Or perhaps there is an exception report that would be needed here?

If you know those answers then you can work around the missing data.  The exact techniques would be different, depending on your exact needs.
0
 
projectsAuthor Commented:
@fishboy2000sd fix seems to be working but Gary added one also.

Guess there is no way of knowing which fix is right yet but the error in the logs have gone away.
I've tested it a few times now. Won't really know until certain conditions arise I guess.

What I need to make sure of is that there is no limiter causing results to be truncated but I won't know that unless I watch for a while I think.
0
 
GaryCommented:
Using @ basically skips the error reporting so you could end with variables that are not correctly set
http://us3.php.net/manual/en/language.operators.errorcontrol.php

My code checks it actually exists in the first place
0
 
projectsAuthor Commented:
Yes, I would prefer not to just hide the problem but to guesstimate a fix for now while keeping an eye on the logs. Gary, your fix took the errors away as well while the data is still flowing as it should for now.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now