2010-03-17 11 views
0

PHP를 사용하여 amazon 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) 

내 목표 같은 몇 가지 결과를 보여 만 기능을 정보]를 얻기 위해 내가 일 does'not

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

를 사용하려고 왜냐하면 그것은 첫 번째 기능 만 보여주기 때문입니다. 모든 기능 정보를 얻으려면 어떻게해야합니까? 제발 도와주세요

답변

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

관련 문제