Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Notice: Undefined Offset

4:10 am on Oct 19, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 6, 2004
votes: 0

The following code...
11 $hrefs = array("one" => array("id"=> "","link" => "test.php?id=one"),
12 "two" => array("id"=>"","link"=>"test.php?id=two"),
13 "three" => array("id" => "", "link" => "test.php?id=three"));
15 for ($i = 0; $i < count($hrefs); $i++) {
15 $href = $hrefs[$i]["link"];
16 if ($this_page == $href) {
17 $hrefs[$i]["id"] = "current";
18 return;
19 }
20 }
...gives this error:
Notice: Undefined offset: 0 in C:\Program Files\Apache Group\Apache2\htdocs\miterbox3\test.php on line 15

It repeats the offset error three times (once for each item in the count($hrefs)).

I hate to admit it, but I have no idea what this means.

I found this on php.net...

$a = "1"; // $a is a string
$a[0] = "f"; // What about string offsets? What happens?

Since PHP (for historical reasons) supports indexing into strings via offsets using the same syntax as array indexing, the example above leads to a problem: should $a become an array with its first element being "f", or should "f" become the first character of the string $a?

The current versions of PHP interpret the second assignment as a string offset identification, so $a becomes "f", the result of this automatic conversion however should be considered undefined.

Their code, not mine.

...but it doesn't help me much. If it's misinterpretting "$href = $hrefs[$i]["link"];", how can I make it understand that I'm trying to reference a certain item in the $hrefs array?

Please forgive my ignorance. I'm used to programming in javascript and suspect that my mistake is tied to that (wring syntax, something of that nature).

Thanks alot for your help and understanding.

6:52 am on Oct 19, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 19, 2003
votes: 0

you could try replacing:

11 $hrefs = array("one" => array("id"=> "","link" => "test.php?id=one"), 
12 "two" => array("id"=>"","link"=>"test.php?id=two"),
13 "three" => array("id" => "", "link" => "test.php?id=three"));


$hrefs = array();
$hrefs[] = array("id"=> "","link" => "test.php?id=one");
$hrefs[] = array("id"=>"","link"=>"test.php?id=two");
$hrefs[] = array("id" => "", "link" => "test.php?id=three");

* saying that the offset is undefined is basically saying that in the array you mention, for that particular 'key' of the array (the part in the [] or [][][]...) it hasn't been set - there's no value for that key, just like you have unset variables.

* php is pretty smart when flipping around data types when it comes to ints, strings, floats, etc., but it's not smart enough with the string 'one' to go searching for

when you create an int in your
loop. It'll be looking for
. If you've got an
, this would probably do the trick (not sure - php is dynamically typed, but I'm not sure how this behavior goes with arrays).

* also remember that most things in php begin with 0. You're beginning with 'one'.

* What the code that I posted does is makes

an array; then it adds to this array those values you see with
$hrefs[] =
. If you use empty brackets, php automatically indexes your array for you - so these will be indexed with the values 0, 1, and 2.

* happy coding and hope this works for you.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members