在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 Object上迭代,并创建一个关联数组,使通过名称引用字段变得容易。我的名字之一是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 ) ) ) 

关于我如何更好地处理这个问题的任何指针,建议或指示…至少,如果有人能弄清楚我怎么才能得到上面的数组,而不用在其他非冗余字段上嵌套数组。非常感谢!

$resultprint_r()CFSimpleXML对象([属性]=> Array([0] => CFSimpleXML对象([Name] => 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
            )
    )
)

,这里是$fieldprint_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;
});
我= CFSimpleXML

http://docs.amazonwebservices.com/AWSSDKforPHP/latest/http://docs.amazonwebservices.com/AWSSDKforPHP/latest/i = CFArray

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吗?

我建议这样做。

更新:

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;
}

我能够让这个工作。希望对你有帮助。

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());
        }
    }
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; 
}

相关内容

  • 没有找到相关文章

最新更新