开发者

Hyphen in Array keys

开发者 https://www.devze.com 2023-01-01 12:35 出处:网络
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:

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
  1. You are missing $ used to create variables in php
  2. It is not a valid array format

.

print $testarray->test-key;
               1
  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

0

精彩评论

暂无评论...
验证码 换一张
取 消