Welcome to WebmasterWorld Guest from 54.227.83.19

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Notice: Undefined Offset

     

createErrorMsg

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

WebmasterWorld Senior Member 10+ Year Member



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"));
14
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...

<?php
$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.
cEM

mincklerstraat

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

WebmasterWorld Senior Member 10+ Year Member



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"));

with:

$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

$array['one']
when you create an int in your
for
loop. It'll be looking for
$array[1]
. If you've got an
$array['1']
, 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

$hrefs
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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month