array dicing cfsimplexml object
I have an array like this
[17] => CFSimpleXML Object
(
[Key] => habteen.mp3
[Size] => 5931725
)
[18] => CFSimpleXML Object
(
[Key] => lawnesareh.mp3
[LastModified] => 2011-05-07T22:24:15.000Z
)
and I want to get the va开发者_C百科lue of Key and size. Howd I go about that?
A CFSimpleXML object is part of the Amazon Webservices PHP api that provides a wrapper around SimpleXML. It has a special constructor that catches unknown function calls and translates them into xpath requests.
The default response is to return another CFSimpleXML object, which oftentimes you simply want the value, so you have to cast it to its proper type.
$key = (string)$array[17]->Key
Another more complex example might look something like this. If you have an known EC2 instance at AWS, and you needed its availability zone heres how that would look:
$description = $ec2->describe_instances(array('InstanceId' => $instance_id));
$availability_zone = (string)$description->body->reservationSet->item->instancesSet->item->placement->availabilityZone;
Where $ec2 is an instance of an AmazonEC2 object and $instance_id is the id of your instance (something like i-6301ea00). The $description that comes back will come back as a CFSimpleXML object, therefore the function calls above will actually translate into a traversal of the XML document to get the availabilityZone.
The following will iterate through the array items and check if the objects have a Key
and Size
property:
foreach($arr as $item)
{
if(isset($item->Key))
{
$item->Key;
}
if(isset($item->Size))
{
$item->Size;
}
}
精彩评论