Results 1 to 4 of 4

Thread: [RESOLVED] [ is invalid?

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Resolved [RESOLVED] [ is invalid?

    Okay, this is probably something very stupid, but I get an error on this line:
    Code:
    $id = (int)$link->query("SELECT id FROM keys WHERE `key`='{$key}' LIMIT 1")[0]['id'];
    saying that the [ is invalid. I can't see why...

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [ is invalid?

    you can't access the array created by your function directly (the way you're trying to). PHP will allow you to access objects in this way, though, if you could rework your code to work like that (assuming it's your own class that has the query function). instead, you'd need:
    PHP Code:
    $query $link->query( ... );
    $id = (int) $query[0]['id']; 
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [ is invalid?

    Thanks!

    Why is that, exactly?

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [ is invalid?

    My guess would be it's just a limit of the language's syntax, but it might have to do with the array not yet existing (or something like that).

    I run into this issue at times, and if I can, I make those functions cast the array they're returning as an object. This won't help in numeric indexed arrays, but for associative arrays it works great:

    PHP Code:
    <?php
      $a 
    = array("test" => "Test!"=> "Test 2!");
      
    $b = (object) $a;

      echo 
    $b->test// prints Test!
      
      
    echo $b->0;   // will throw an error
      // syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$'
      
      
    echo $b->{0}; // also throws an error
      // Undefined property: stdClass::$0
    ?>
    <pre><?php print_r($b); ?></pre>
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width