Home php “Illegal string offset 'id'” when trying to access an array element

“Illegal string offset ‘id’” when trying to access an array element

Author

Date

Category

Here is a piece of PHP code:

public function getAllianceRank ($ id) {
        $ this- & gt; procARankArray ();
        while (1) {
          if (count ($ this- & gt; rankarray) & gt; 1) {
            $ key = key ($ this- & gt; rankarray);
            if ($ this- & gt; rankarray [$ key] ["id"] == $ id) {// THIS LINE HAS A WARNING
              return $ key;
              break;
            } else {
              if (! next ($ this- & gt; rankarray)) {
                return false;
                break;
              }
            }
          } else {
            return 1;
          }
        }
      }

And here is the warning itself:

Warning: Illegal string offset 'id' in /var/www/GameEngine/Ranking.php on line 189

As I understand it, there should be an array, and there should be a string, right? How do I fix this warning?


Answer 1, authority 100%

if (is_array ($ this- & gt; rankarray [$ key]) & amp; & amp; $ this- & gt; rankarray [$ key] ["id"] == $ id)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions