Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is my example array:

$arrayy[0]=48.72;
$arrayy[1]=21.32;
$arrayy[2]=48.62;
$arrayy[3]=21.31;
$arrayy[4]=48.62;
$arrayy[5]=21.31;

This function

function writeDouble($array){
        for($curr = 0; $curr<count($array)-1; $curr++){
            echo $array[$curr]." - ";
            echo $array[$curr+1]."<br>";
            $curr++;
    }
}

should write a couples (0-1 , 2-3 , 4-5) - an output like:

48.72 - 21.32
48.62 - 21.31
48.62 - 21.31

What am I doing wrong, why do I got an error?

Notice: Undefined offset: 6 in C:\xampp\htdocs\xampp\lg\functions.php on line 466

Or could you define a better function to make couples? I can't think anymore... thanks

share|improve this question
 
Yes. They are always even... hmm, ok. But why do I have this offset on my server? –  user1666761 Sep 16 '12 at 17:20
 
Your code works fine according to this: codepad.org/cHdWSMMm. –  Kenny Sep 16 '12 at 17:24
 
What's the code on line 466 in your functions.php? –  Kenny Sep 16 '12 at 17:29
add comment

3 Answers

up vote 0 down vote accepted
  1. You're using $array[$curr + 1] but you're iterating from 0 to $curr - 1. You need an isset in case you have an odd number of values in your array.

  2. You're incrementing 2 times (one time in your for, one time in the scope of your for).

Code solution:

$arrayy[0]=48.72;
$arrayy[1]=21.32;
$arrayy[2]=48.62;
$arrayy[3]=21.31;
$arrayy[4]=48.62;
$arrayy[5]=21.31;    

function writeDouble($array) {
        for ($curr = 0; $curr < (count($array) - 1); $curr += 2) {
            echo $array[$curr] . " - ";
            if (isset($array[$curr + 1])) {
              echo $array[$curr + 1];
            }
            echo "<br>";
    }
}

writeDouble($arrayy);

Output:

48.72 - 21.32
48.62 - 21.31
48.62 - 21.31

No more warning.

share|improve this answer
 
Your code doesn't work well. The third "row" is not showing. –  user1666761 Sep 16 '12 at 17:30
 
updated my answer after checking –  Alain Tiemblo Sep 16 '12 at 17:36
 
Ok, works.. as my example. Because warning was from different function :-) I'm kinda distracted today, thanks –  user1666761 Sep 16 '12 at 17:39
add comment

Because in the last iteration in line echo $array[$curr+1]."<br>"; you'll be looking for $array[count($array)] which is ofcource not defined!!

share|improve this answer
 
I subtracted "1" (-1) from size of cycle count($array) . –  user1666761 Sep 16 '12 at 17:19
 
@user1666761 - But in the last iteration, $curr will be count($array)-1, which means $curr+1 will be one greater than count($array)-1 –  Mark Baker Sep 16 '12 at 17:23
 
So, what do you suggest? After removing -1, error still stays. –  user1666761 Sep 16 '12 at 17:25
 
iterating until count($array) -2 –  artragis Sep 16 '12 at 18:02
add comment

Note that you are incrementing $curr two times:

for($curr = 0; $curr<count($array)-1; $curr++){

and

$curr++

This is the reason for going out of range in your loop

share|improve this answer
 
Because I need to do couples. So increment need to be by 2. –  user1666761 Sep 16 '12 at 17:31
 
Oh I understand. And thinking it better this is not the reason for an undefined index. Make a var_dump($array) and check if the array is built as you state in your question. –  Hernan Velasquez Sep 16 '12 at 17:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.