plist simpleXML,访问数组列表时遇到问题

时间:2014-12-22 17:18:17

标签: php xml plist

假设我有一个plist simplexml文档,如下所示:

<dict>
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>armv7</string>
</array>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

我可以像这样访问一个字符串:

$obj->dict->string[0]

这将为我返回6.0。但是,如果我想访问第二个数组中的第一个字符串:

$obj->dict->array[1]->string[0]

PHP抛出错误,不喜欢我对数组的引用。这里的语法是什么?关于apple plist simplexml文档的例子并不多。感谢。

1 个答案:

答案 0 :(得分:1)

由于arrayPHP keyword,因此尝试在该上下文中使用它会出现语法错误。您需要做的是将其包装为{}内的引用字符串,从而将其有效地转换为动态属性名称。

// Using the {"string"} dynamic property syntax:
echo $obj->dict->{'array'}[1]->string[0]
// Prints UIInterfaceOrientationPortrait

PHP variable variables, and variable properties syntax引用中含糊不清。

除了避免使用关键字之外,您可以使用它来动态地构造属性作为字符串。它很方便,但并不是众所周知的。

// More often used to build properties or method names as strings...
///...Not that you need to do this...
$v1 = "arr";
$v2 = "ay";
echo $obj->dict->{$v1 . $v2}[1]->string[0];
相关问题