3

Here is what my object looks like with print_r (this is an object returned by the PHP SDK for the 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
                            )

                    )

            )

I have a function that iterates over the GetAttributesResult Object and creates an associative array that makes it easy to reference my fields by their names. One of my Names is data_tags, which is repeated an unknown number of times. I would like to return data_tags as a simple indexed array of those values. Here's my function, which doesn't work.

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

I don't know if this is the most elegant solution, but I don't see why it wouldn't work. array_key_exists doesn't return true. By mistake I was able to test as in_array($field-Name,$results[$x]) and that built the array of my repeated $field->Name values... but it also converted all of the other values into single item nested array... so it would seem that it returned true more than I thought it would have. Although the hyphen in there was by mistake I meant to use -> which doesn't return true... so I'm very confused by what is going on there. Here's the print_r to show what came back.

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 ) ) ) 

Any pointers, suggestions or instruction on how I might handle this better... and at very least if someone can figure out how I can get to the above array without the nested arrays on the other non-redundant fields. Very much appreciated!

Here is the print_r() of $result 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_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
            )

    )

)

and here is a print_r() of $field (iterated and separated by <hr> tags.)

  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
  )
Ecropolis
  • 2,005
  • 2
  • 22
  • 27

5 Answers5

8

In the AWS PHP SDK, you can use to_json(), to_stdClass() and even to_array() to get back other data types from a CFSimpleXML object. Also with SimpleXML objects, typecasting is key!

PHP has an object called ArrayObject which is more-or-less an OOP version of an array. When you call CFSimpleXML->to_array(), you get back a CFArray object, which wraps the native ArrayObject object with extra functionality.

$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

Skyler Johnson
  • 3,833
  • 1
  • 20
  • 6
  • if to_json etc work anything like json_encode, I kinda doubt it's actually consistently going to give a json representation of XML. Doing so while preserving attributes will always be either very ugly or inconsistent. – nairbv Aug 17 '12 at 09:04
0

I was able to get this one to work. Hope this helps.

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

    }
Abishek
  • 11,191
  • 19
  • 72
  • 111
0

enter code hereDo you mean something like this:

$data_tags = array();
foreach ( $select->body->GetAttributesResult AS $attr ) {
  if ( $attr->Name == 'data_tags' ) {
    $data_tags[] = $attr->Value;
  }
}

Otherwise, I don't know what you want =)

edit
Are you sure GetAttributesResult is right? Don't you mean http://www.php.net/manual/en/simplexmlelement.attributes.php?

Rudie
  • 52,220
  • 42
  • 131
  • 173
  • GetAttributesResult is the name of the object that is returned by an SDK I'm using. I wouldn't want to 'hard code' 'data_tags' into the logic unless I have to. Any redundant name -- I would like to convert to an array. – Ecropolis Apr 25 '11 at 14:00
0

I would suggest something like that.

UPDATED:

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;
}
Eugene
  • 4,352
  • 8
  • 55
  • 79
  • thank you. looks like it should work... but I'm only getting the last tag_name and the value isn't getting converted to an array so the array_key_exits must not be evaluating to true. – Ecropolis Apr 25 '11 at 14:36
  • Could you add a `print_r()` of `$result` and `$field` as I asked before and add them to your main post? The would have better understaing and I could suggest something, that works. – Eugene Apr 25 '11 at 14:46
  • Thanks. My latest attempt looks like this. I thought maybe check if the value is_array and if it isn't save the value that is in it; convert it, store it.... make sense..? array_key_exists seems to fail. `if ( $field && array_key_exists( $field->Name, $results[ $x ] ) ) { if ( ! is_array( $results[ $x ][$field->Name] ) ) { $val = $results[ $x ][$field->Name]; $results[ $x ] = array($field->Name = $val); } $results[ $x ][ (string) $field->Name ][] = (string) $field->Value; //return 'true'; }` – Ecropolis Apr 25 '11 at 17:48
  • I did add the `print_r()` results you requested. – Ecropolis Apr 25 '11 at 18:06
  • added typecast (string) to the array_key_exits() func and that got evals to true after that... so I'm making progress – Ecropolis Apr 25 '11 at 19:28
  • I am going to post what I ended up with because it worked and its a little bit different from what you added. – Ecropolis Apr 27 '11 at 18:31
  • Should I have edited your post; rather than post a new one, since you did most of the solving? I think the only real problem was preserving the non-array value from the first iteration, converting to array, storing that first val. – Ecropolis Apr 27 '11 at 21:50
-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; 
}
Ecropolis
  • 2,005
  • 2
  • 22
  • 27
  • And what will happen if during conversion `check` will be an empty string and `field` is not set. It will probably trigger illegal offset. – Eugene Apr 28 '11 at 05:34