[英]PHP : How to get specific data from array
我嘗試通過PHP使用Amazon API。 如果我用
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
它對我不起作用,因為它僅向我顯示了第一個功能。 我如何獲得所有功能信息? 請幫忙
在您的代碼中, $feature
應該包含一個數組。 您可以遍歷所有這些功能:
foreach($feature as $f) {
echo $f;
}
還是您想要所有商品的功能?
遍歷所有項目並將Feature屬性放入數組中?
$feature = array();
foreach($parsed_xml->Items as $item)
{
$feature[] = $item->ItemAttributes->Feature;
}
在我看來,要素元素實際上是一個數組,您只需要對其進行迭代
// 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";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.