|
-
Dec 18th, 2010, 10:58 PM
#1
Thread Starter
Stack Overflow moderator
[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...
-
Dec 19th, 2010, 01:50 AM
#2
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'];
-
Dec 19th, 2010, 03:24 PM
#3
Thread Starter
Stack Overflow moderator
Re: [ is invalid?
Thanks!
Why is that, exactly?
-
Dec 19th, 2010, 04:28 PM
#4
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!", 0 => "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>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|