这是我的对象与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值的数组......但是它还将所有其他值转换为单项嵌套数组...所以它似乎它的回报比我想象的还要多。虽然那里的连字符是错误的我打算使用 - >这不会返回真实...所以我对那里发生的事情感到很困惑。这是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 ) ) )
关于如何更好地处理这个问题的任何指针,建议或说明......至少如果有人能够弄清楚如何在没有其他非冗余字段的嵌套数组的情况下找到上述数组。非常感谢!
以下是print_r()
的{{1}}
CFSimpleXML对象
(
[Attribute] =>排列
(
[0] => CFSimpleXML对象
(
[姓名] => data_datein
[值] => 2011-04-23
)
$result
此处是 [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
)
)
)
print_r()
(由$field
标记迭代和分隔。)
<hr>
答案 0 :(得分:8)
在AWS PHP SDK中,您可以使用to_json(),to_stdClass()甚至to_array()从CFSimpleXML对象中获取其他数据类型。对于SimpleXML对象,类型转换也很关键!
PHP有一个名为ArrayObject的对象,它或多或少是一个数组的OOP版本。当您调用CFSimpleXML-&gt; 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
答案 1 :(得分:0)
enter code here
你的意思是这样的:
$data_tags = array();
foreach ( $select->body->GetAttributesResult AS $attr ) {
if ( $attr->Name == 'data_tags' ) {
$data_tags[] = $attr->Value;
}
}
否则,我不知道你想要什么=)
修改强>
你确定GetAttributesResult
是对的吗?你的意思是http://www.php.net/manual/en/simplexmlelement.attributes.php吗?
答案 2 :(得分:0)
我会建议这样的事情。
<强>更新:强>
function getAttributesIntoArray( $select )
{
$results = array();
$x = 0;
foreach ( $select->body->GetAttributesResult as $result )
{
foreach ( $result as $field )
{
if ( ! isset( $results[ $x ] ) )
{
$results[ $x ] = array();
}
// Assuming, that if the $field->Value is array, then it probably have only one element
if ( $field )
{
// or if ( isset( $results[ $x ][ (string) $field->Name ] ) ) instead of array_key_exists
if ( array_key_exists( (string) $field->Name, $results[ $x ] ) )
{
$results[ $x ][ (string) $field->Name ][] = ( is_array( $field->Value ) ) ? $field->Value[0] : $field->Value;
}
else
{
$results[ $x ][ (string) $field->Name ] = ( is_array( $field->Value ) ) ? $field->Value[0] : $field->Value;
}
}
}
$x++;
}
return $results;
}
答案 3 :(得分:0)
我能够让这个工作。希望这会有所帮助。
protected function CFResponseToArray($response)
{
try {
if ($response->isOK()) {
$responseObj = $response->body->to_array()->getArrayCopy();
//log_message('info', print_r($responseObj, true));
$result = array();
if (isset($responseObj['SelectResult']['Item'])) {
if (is_array($responseObj['SelectResult']['Item'])) {
if (isset($responseObj['SelectResult']['Item']['Name'])) {
$itemObj = array();
//log_message('info', print_r($responseObj['SelectResult'], true));
$resultItem = $responseObj['SelectResult']['Item'];
$itemObj['Id'] = $resultItem['Name'];
$attributes = $resultItem['Attribute'];
for ($i = 0; $i < count($attributes); $i++) {
$itemObj[$attributes[$i]['Name']] = $attributes[$i]['Value'];
}
$result[] = $itemObj;
} else {
//log_message('info', print_r($responseObj['SelectResult'], true));
foreach ($responseObj['SelectResult']['Item'] as $resultItem) {
$itemObj = array();
$itemObj['Id'] = $resultItem['Name'];
$attributes = $resultItem['Attribute'];
for ($i = 0; $i < count($attributes); $i++) {
$itemObj[$attributes[$i]['Name']] = is_array($attributes[$i]['Value']) ? "" : $attributes[$i]['Value'];
}
$result[] = $itemObj;
}
if (isset($responseObj['SelectResult']['NextToken'])) {
$this->nextToken = $responseObj['SelectResult']['NextToken'];
} else {
$this->nextToken = '';
}
}
}
}
return $result;
}
} catch (exception $ex) {
log_message('error', $ex->getMessage());
}
}
答案 4 :(得分:-1)
function attrToArray($select) {
$results = array();
$x = 0;
foreach ( $select->body->GetAttributesResult as $result ) {
foreach ( $result as $field ) {
$check = (string) $field->Name;
if (isset($field) && array_key_exists($check, $results[$x] ) ) {
if ( ! is_array( $results[ $x ][$check] ) ) {
$val = (string) $results[ $x ][$check];
$results[ $x ][ $check ] = array();
$results[ $x ][ $check ][] = $val;
}
$results[ $x ][ $check ][] = (string) $field->Value;
} else {
$results[ $x ][ $check ] = (string) $field->Value;
}
}
$x++;
}
return $results;
}