迭代SimpleXML Objext PHP
作者:互联网
这是我的对象在print_r中的样子(这是由PHP SDK为Amazon Web Services Simple DB返回的对象.
[GetAttributesResult] => CFSimpleXML Object
(
[Attribute] => Array
(
[0] => CFSimpleXML Object
(
[Name] => data_datein
[Value] => 2011-04-23
)
[1] => CFSimpleXML Object
(
[Name] => data_estatus
[Value] => 0
)
[2] => CFSimpleXML Object
(
[Name] => data_status
[Value] => 1
)
[3] => CFSimpleXML Object
(
[Name] => data_title
[Value] => Company Info
)
[4] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => firsttag
)
[5] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => secondtag
)
[6] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => thirdtag
)
[7] => CFSimpleXML Object
(
[Name] => data_files
[Value] => company_info.flv
)
[8] => CFSimpleXML Object
(
[Name] => data_id
[Value] => 8993
)
)
)
我有一个迭代GetAttributesResult对象的函数,并创建一个关联数组,可以很容易地按名称引用我的字段.我的一个名字是data_tags,它重复了未知的次数.我想将data_tags作为这些值的简单索引数组返回.这是我的功能,不起作用.
function attrToArray($select) {
$results = array();
$x = 0;
foreach($select->body->GetAttributesResult as $result) {
foreach ($result as $field) {
if (array_key_exists($field,$results[$x])) {
$results[$x][ (string) $field->Name ][] = (string) $field->Value;
} else {
$results[$x][ (string) $field->Name ] = (string) $field->Value;
}
}
$x++;
}
return $results;
}
我不知道这是否是最优雅的解决方案,但我不明白为什么它不起作用. array_key_exists不返回true.错误地,我能够测试为in_array($field-Name,$results [$x])并构建了我重复的$field-> Name值的数组……但它也将所有其他值转换为单项嵌套数组…所以它似乎比我想象的更多返回true.虽然那里的连字符是错误的我打算使用 – >这不会返回真实…所以我对那里发生的事情感到很困惑.这是print_r来显示返回的内容.
Array ( [0] => Array (
[data_datein] => 2011-04-23
[data_estatus] => 0
[data_status] => Array ( [0] => 1 )
[data_title] => Array ( [0] => Company Info )
[data_tags] => Array (
[0] => firsttag
[1] => secondtag
[2] => thirdtag )
[data_files] => Array ( [0] => company_info.flv )
[data_id] => Array ( [0] => 8993 ) ) )
关于如何更好地处理这个问题的任何指针,建议或说明……至少如果有人能够弄清楚如何在没有其他非冗余字段的嵌套数组的情况下找到上面的数组.非常感谢!
这是$result的print_r()
CFSimpleXML对象
(
[Attribute] =>排列
(
[0] => CFSimpleXML对象
(
[姓名] => data_datein
[值] => 2011-04-23
)
[1] => CFSimpleXML Object
(
[Name] => data_estatus
[Value] => 0
)
[2] => CFSimpleXML Object
(
[Name] => data_title
[Value] => 0001 01 Company Name
)
[3] => CFSimpleXML Object
(
[Name] => data_status
[Value] => 1
)
[4] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => good stuff
)
[5] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => save tags
)
[6] => CFSimpleXML Object
(
[Name] => data_tags
[Value] => tagger works
)
[7] => CFSimpleXML Object
(
[Name] => data_files
[Value] => 0001_01_company_name.flv
)
[8] => CFSimpleXML Object
(
[Name] => data_id
[Value] => yFKwIxjIhH
)
)
)
这是$field的print_r()(迭代并用< hr>标记分隔.)
CFSimpleXML Object
(
[Name] => data_datein
[Value] => 2011-04-23
)
<hr>CFSimpleXML Object
(
[Name] => data_estatus
[Value] => 0
)
<hr>CFSimpleXML Object
(
[Name] => data_title
[Value] => 0001 01 Company Name
)
<hr>CFSimpleXML Object
(
[Name] => data_status
[Value] => 1
)
<hr>CFSimpleXML Object
(
[Name] => data_tags
[Value] => good stuff
)
<hr>CFSimpleXML Object
(
[Name] => data_tags
[Value] => save tags
)
<hr>CFSimpleXML Object
(
[Name] => data_tags
[Value] => tagger works
)
<hr>CFSimpleXML Object
(
[Name] => data_files
[Value] => 0001_01_company_name.flv
)
<hr>CFSimpleXML Object
(
[Name] => data_id
[Value] => yFKwIxjIhH
)
解决方法:
在AWS PHP SDK中,您可以使用to_json(),to_stdClass()甚至to_array()从CFSimpleXML对象中获取其他数据类型.对于SimpleXML对象,类型转换也很关键!
PHP有一个名为ArrayObject的对象,它或多或少是一个数组的OOP版本.当您调用CFSimpleXML-> to_array()时,您将获得一个CFArray对象,该对象使用额外的功能包装本机ArrayObject对象.
$array = $response->body->GetAttributesResult->to_array();
list($name, $value) = $array['Attribute']->first()->map(function($node, $i) {
return (string) $node;
});
http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#i=CFSimpleXML
http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#i=CFArray
标签:php,arrays,amazon-web-services,simplexml,amazon-simpledb 来源: https://codeday.me/bug/20190730/1578756.html