Hyphen in Array keys
All,
I have an array with hyphens in the key name. How do I extract the value of it in PHP? It returns a 0 to me, if I access like this:
print $testarray->test-key;
This is how the array looks li开发者_开发问答ke
testarray[] = {["test-key"]=2,["hotlink"]=1}
Thanks
You have problems:
testarray[] = {["test-key"]=2,["hotlink"]=1}
1 2
- You are missing
$
used to create variables in php - It is not a valid array format
.
print $testarray->test-key;
1
- The
=>
operator is used for objects, not arrays, use[]
instead.
Here is how your code should be like:
$testarray = array("test-key" => 2, "hotlink" => 1);
print $testarray['test-key'];
Finally,
See PHP Array Manual
print $testarray["test-key"];
The PHP manual has a good page explaining arrays and how to do things with them: http://www.php.net/manual/en/language.types.array.php
精彩评论