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
)