PHP:如何从数组中获取特定数据

发布于 2024-08-25 16:19:36 字数 2209 浏览 3 评论 0原文

我尝试使用 PHP 来使用亚马逊 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

它对我不起作用,因为它只向我显示第一个功能。如何获取所有功能信息?请帮忙

I try to use amazon API using PHP. If I use

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

it show me some result like

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 ) 

my goal is to get only Feature infomation and I try to use

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

it does'not work for me because it just show me the first feature only. How do i get all feature information? please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白日梦 2024-09-01 16:19:36

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

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

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

In your code $feature should contain an array. You can iterate over all this features:

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

Or do you want the features of all items?

时光磨忆 2024-09-01 16:19:36

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

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

Loop through the items and put the Feature attribute in an array?

$feature = array();
foreach($parsed_xml->Items as $item)
{
 $feature[] = $item->ItemAttributes->Feature;
}
苏大泽ㄣ 2024-09-01 16:19:36

在我看来,特征元素实际上是一个数组,您只需要迭代它

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

Looks to me like the feature element is actually an array, you'll just need to iterate through it

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

or if you want to show the index of the feature (which is just 0-n in this case)

// loop through each feature in the features array
foreach($features as $key => $feature) {
    echo "$key. $feature\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文