PHP ERROR: Illegal offset type

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Quote:
Warning: Illegal offset type in /home2/travislp/public_html/users/bsa/private/class/admin.class.php on line 436


Okay, so just randomly while a guy was using one of my scripts he gets these errors. I looked them up and all php say's is,
Quote:
You cannot use arrays or objects as keys. Doing so will result in a warning: Illegal offset type.


I dont really know what that means and The script worked fine before. The strange part is that this only occurs during a certain part of the script. Then it works fine.

PHP Code: [ Select ]
$badges[][][] = array();
         $Keys = array("A","B","C","D","E",
                        "F","G","H","I","J",
                        "K","L","M","N","O",
                        "P","Q","R","S","T",
                        "U","V","W","X","Y",
                        "Z");
         
         
         $query = @mysql_query("SELECT * FROM meritbadge ORDER BY meritbadgeNAME");
         while($myrow = mysql_fetch_array($query)) {          
              $letters = str_split($myrow['meritbadgeNAME']);
            for($i=0;$i<26;$i++) {
               if($Keys[$i]==$letters[0]) {
                  $badges[$i][0][0]++;
                  $count = $badges[$i][0][0];
                  $badges[$i][$count][0] = $myrow['meritbadgeNAME']; //436
                  if($myrow['meritbadgeEAGLE'] == 1) {
                     $badges[$i][$count][1] = 1;   //438
                  } else {
                     $badges[$i][$count][1] = 0;     //440
                  }
                  $badges[$i][$count][2] = $myrow['meritbadgeID'];  //442
                 
                 
               }
            }  
         }
  1. $badges[][][] = array();
  2.          $Keys = array("A","B","C","D","E",
  3.                         "F","G","H","I","J",
  4.                         "K","L","M","N","O",
  5.                         "P","Q","R","S","T",
  6.                         "U","V","W","X","Y",
  7.                         "Z");
  8.          
  9.          
  10.          $query = @mysql_query("SELECT * FROM meritbadge ORDER BY meritbadgeNAME");
  11.          while($myrow = mysql_fetch_array($query)) {          
  12.               $letters = str_split($myrow['meritbadgeNAME']);
  13.             for($i=0;$i<26;$i++) {
  14.                if($Keys[$i]==$letters[0]) {
  15.                   $badges[$i][0][0]++;
  16.                   $count = $badges[$i][0][0];
  17.                   $badges[$i][$count][0] = $myrow['meritbadgeNAME']; //436
  18.                   if($myrow['meritbadgeEAGLE'] == 1) {
  19.                      $badges[$i][$count][1] = 1;   //438
  20.                   } else {
  21.                      $badges[$i][$count][1] = 0;     //440
  22.                   }
  23.                   $badges[$i][$count][2] = $myrow['meritbadgeID'];  //442
  24.                  
  25.                  
  26.                }
  27.             }  
  28.          }



From what I can see it doesnt like when $i = 0, this being the 'A'. After a's the script works fine.
Moderator Remark: removed repeated error lines
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • onlyican.com
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1589
  • Loc: Hants, UK

Post 3+ Months Ago

Illegal offset threw me at first

This will give an illegal offset
Code: [ Select ]
<?php
$MyArray = array("A" > "SomeValue", "B" > "Another Value");

echo $MyArray["C"];
?>
  1. <?php
  2. $MyArray = array("A" > "SomeValue", "B" > "Another Value");
  3. echo $MyArray["C"];
  4. ?>

As C is not in the array, it is an illegal offset

So now
Soluition to stop this on Dynamic Arrays

Code: [ Select ]

<?php
if(isset($MyArray["C"])){
echo $MyArray["C"];
}
?>
  1. <?php
  2. if(isset($MyArray["C"])){
  3. echo $MyArray["C"];
  4. }
  5. ?>


Your problem is $badges is a blank array
So therefor
$count = $badges[$i][0][0]; Does not exists

and
$badges[$i][$count][1] Dont Exist
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Alright, thanks for that info.
Solved it right up.
:)

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 78 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.