2010-03-17 153 views
0

我尝试使用PHP使用亚马逊API。如果我使用PHP:如何从数组中获取特定数据

print_r($parsed_xml->Items->Item->ItemAttributes) 

拿给我喜欢

SimpleXMLElement Object ( 
[Binding] => Electronics 
[Brand] => Canon 
[DisplaySize] => 2.5 
[EAN] => 0013803113662 ** 
[Feature] => Array ( 
[0] => High-powered 20x wide-angle optical zoom with Optical Image Stabilizer 
[1] => Capture 720p HD movies with stereo sound; HDMI output connector for easy playback on your HDTV 
[2] => 2.5-inch Vari-Angle System LCD; improved Smart AUTO intelligently selects from 22 predefined shooting situations 
[3] => DIGIC 4 Image Processor; 12.1-megapixel resolution for poster-size, photo-quality prints 
[4] => Powered by AA batteries (included); capture images to SD/SDHC memory cards (not included))** 
[FloppyDiskDriveDescription] => None 
[FormFactor] => Rotating 
[HasRedEyeReduction] => 1 
[IsAutographed] => 0 
[IsMemorabilia] => 0 
[ItemDimensions] => SimpleXMLElement Object ( 
[Height] => 340 
[Length] => 490 
[Weight] => 124 
[Width] => 350) 
[Label] => Canon 
[LensType] => Zoom lens 
[ListPrice] => SimpleXMLElement Object ( 
[Amount] => 60103 
[CurrencyCode] => USD 
[FormattedPrice] => $601.03) 
[Manufacturer] => Canon 
[MaximumFocalLength] => 100 
[MaximumResolution] => 12.1 
[MinimumFocalLength] => 5 
[Model] => SX20IS 
[MPN] => SX20IS 
[OpticalSensorResolution] => 12.1 
[OpticalZoom] => 20 
[PackageDimensions] => SimpleXMLElement Object ( 
[Height] => 460 
[Length] => 900 
[Weight] => 242 
[Width] => 630) 
[PackageQuantity] => 1 
[ProductGroup] => Photography 
[ProductTypeName] => CAMERA_DIGITAL 
[ProductTypeSubcategory] => point-and-shoot 
[Publisher] => Canon 
[Studio] => Canon 
[Title] => Canon PowerShot SX20IS 12.1MP Digital Camera with 20x Wide Angle Optical Image Stabilized Zoom and 2.5-inch Articulating LCD 
[UPC] => 013803113662) 

我的目标有些结果是只得到功能信息来源,我尝试使用

$feature = $parsed_xml->Items->Item->ItemAttributes->Feature 

它does'not工作对我而言,因为它只是向我展示第一个功能。我如何获取所有功能信息?请帮助

回答

0

在你的代码$feature应该包含一个数组。您可以遍历所有这些功能:

foreach($feature as $f) { 
    echo $f; 
} 

或者您是否想要所有项目的功能?

0

循环遍历项目并将Feature属性放入数组中?

$feature = array(); 
foreach($parsed_xml->Items as $item) 
{ 
$feature[] = $item->ItemAttributes->Feature; 
} 
0

我看来像特征要素实际上是一个数组,你只需要通过它

// assign the feature array to a var to prevent reading the xml each loop iteration 
$features = $parsed_xml->Items->Item->ItemAttributes->Feature 

// loop through each feature in the features array 
foreach($features as $feature) { 
    echo "* $feature\n"; 
} 

迭代,或者如果你想显示功能的索引(这仅仅是在这种情况下为0-n)

// loop through each feature in the features array 
foreach($features as $key => $feature) { 
    echo "$key. $feature\n"; 
} 
+0

这当然如果你正在寻找你提供的一个元素的功能,如果你需要获得所有元素的功能,你只需要将我的答案和Alxwest的回答是,创建一个嵌套的foreach循环。 – Rabbott 2010-03-17 15:44:00