0

The index 4 exisits in array rating but i get error...if i undo the comments $write=4 then it works fine.

foreach($writers[$i] as $write)
      {
      echo "writer: $write  -  rating: ";
      print_r($rating);
//$write=4;
      echo "<br>". $rating[$write] ;

}

the above code gives

    writer: 4 
Notice: Undefined index: 4 in D:\wamp\www\shazia\CRM\EffortTrackUpload\admin\cron.php on line 232

The array rating gives:

Array ( [3] => 5.1 [4] => 6 [5] => 5.2 [6] => 5 [8] => 5 [9] => 5 [10] => 5 [11] => 4 [12] => 3.6 [13] => 5 [14] => 5.1 )

can anyone please help me explain what i am doing wrong.

Thanks

10
  • var_dump($rating); --- If php says there is no item 4 - then there is not. Check once again. Commented Mar 8, 2011 at 6:06
  • Here is what the var_dump returned:it does have index 4: array(11) { [3]=> string(3) "5.1" [4]=> string(1) "6" [5]=> string(3) "5.2" [6]=> string(1) "5" [8]=> string(1) "5" [9]=> string(1) "5" [10]=> string(1) "5" [11]=> string(1) "4" [12]=> string(3) "3.6" [13]=> string(1) "5" [14]=> string(3) "5.1" } Commented Mar 8, 2011 at 6:09
  • Just checking if you uncomment your assignment to $write and make it like this: $write="4"; Does it still print 6? Commented Mar 8, 2011 at 6:11
  • better yet do a var_dump on $write Commented Mar 8, 2011 at 6:11
  • 3
    $write should equal "4" not " 4" Commented Mar 8, 2011 at 6:15

4 Answers 4

2

As per your comment, the issue is that $write is a string - " 4", but the array index is just an integer 4.

Work out why the 4 is padded with space in the $writers[$i] array, if you are getting the array from elsewhere you could use intval($write) to get the correct value.

Sign up to request clarification or add additional context in comments.

Comments

1

Either $writers[$i] is undefined when $i = 4, or $rating[$write] is undefined when $write = 4. It will be whichever one is on line 232 corresponding to your error message.

Use if(isset($writers[$i])) to prevent the error.

Edit From your comment, I'd say it is $rating[4] that is undefined.

Comments

0
foreach($writers[$i] as $write)
      {
      echo "writer: $write  -  rating: ";
      print_r($rating);
//$write=4;
      echo "<br>". $rating[trim($write)] ; // use trim so that it will replace " 4" to "4"

}

Comments

-1
foreach($writers as $write){
   //code here
}

Is how it should be written :)

4 Comments

$writers is a multidimentional array
OPs foreach, I'm assuming is nested within another loop.
@Russel Dias yes it is a nested
Forgive me, i assumed that it was not nested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.