/    Sign up×
Community /Pin to ProfileBookmark

foreach loop to return key => value from XML file?

so i’ve followed a few questions on here to try and get this to work….but i just can’t seem to crack it. essentially what i’m trying to do is loop through an XML file and return key => value pairs for elements from that file. For a bit more context, i’m trying to store an item’s ASIN (amazon API) as the key and it’s image URL as the value in an array.

here’s the structure of the XML file, in case these helps someone determine where my code is flawed:

[CODE]<Items>
<Request>
<IsValid>True</IsValid>
<ItemSearchRequest>
<Keywords>biology</Keywords>
<ResponseGroup>Images</ResponseGroup>
<SearchIndex>Books</SearchIndex>
</ItemSearchRequest>
</Request>
<TotalResults>280951</TotalResults>
<TotalPages>28096</TotalPages>
<MoreSearchResultsUrl>
http://www.amazon.com/gp/redirect.html?camp=2025&creative=386001&location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fsearch%3Fkeywords%3Dbiology%26url%3Dsearch-alias%253Dstripbooks&linkCode=xm2&tag=comparcom035-20&SubscriptionId=AKIAIQ7UEDX4CFRSBDXA
</MoreSearchResultsUrl>
<Item>
<ASIN>0321558235</ASIN>
<SmallImage>
<URL>
http://ecx.images-amazon.com/images/I/41UWC4kbxGL._SL75_.jpg
</URL>
<Height Units=”pixels”>75</Height>
<Width Units=”pixels”>62</Width>
</SmallImage>[/CODE]

and here’s the code i’m working with:

[code=php]$xml = simplexml_load_file($SignedRequest);
$image = simplexml_load_file($getimage);

foreach ($image->Items->Item->ASIN as $key => $value) {
$array[$key] = $image->Items->Item->SmallImage->URL;

}

print_r($array);[/code]

my output looks like this:

Array ( [ASIN] => SimpleXMLElement Object ( [0] => [url]http://ecx.images-amazon.com/images/I/41UWC4kbxGL.SL75.jpg[/url] ) )

I think i’m close…..but can anyone point out what i’m doing wrong here? I can’t seem to get my loop to assign the key and value properly without throwing some kind of error.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 11.2014 — In playing around with it, I found I had to force the key and value to be strings (instead of XML objects):
[code=php]
<pre>
<?php
$text = <<<EOD
<Items>
<Request>
<IsValid>True</IsValid>
<ItemSearchRequest>
<Keywords>biology</Keywords>
<ResponseGroup>Images</ResponseGroup>
<SearchIndex>Books</SearchIndex>
</ItemSearchRequest>
</Request>
<TotalResults>280951</TotalResults>
<TotalPages>28096</TotalPages>
<MoreSearchResultsUrl>
long URL goes here
</MoreSearchResultsUrl>
<Item>
<ASIN>0321558235</ASIN>
<SmallImage>
<URL>
http://ecx.images-amazon.com/images/I/41UWC4kbxGL._SL75_.jpg
</URL>
<Height Units="pixels">75</Height>
<Width Units="pixels">62</Width>
</SmallImage>
</Item>
</Items>
EOD;

$xml = simplexml_load_string($text);
$array = array();
foreach ($xml->Item as $item) {
$array[(string)$item->ASIN] = trim((string)$item->SmallImage->URL);
}
print_r($array);
?>
</pre>
[/code]
Copy linkTweet thisAlerts:
@ivanwakeupauthorNov 11.2014 — ah, indeed. this is the code that actually did it for me:

<PHP>foreach ($image->Items->Item as $key => $value) {

$array1[(string)$image->Items->Item->ASIN] = (string) $image->Items->Item->SmallImage->URL;

}


</PHP>

unfortunately now im stuck on a different problem....only the first key value pair in the array is displaying under the print_r. this particular XML file has about ~10 ASINs. any ideas on why this loop isn't iterating through everything?
Copy linkTweet thisAlerts:
@NogDogNov 11.2014 — I think I'd have to see the actual XML being parsed to understand the structure. Can there be more than one ASIN per Item?
Copy linkTweet thisAlerts:
@ivanwakeupauthorNov 11.2014 — ahh, i found out what was causing me problems:

having my foreach loop defined like this:

[CODE]foreach ($xml->Items->Item as $key => $value) {

...some code

}[/CODE]


for some reason, this wasn't iterating through every $item. I didn't realize i could specify key => value pairs WITHOUT that particular syntax. rewriting the loop like this:

[CODE]foreach ($image->Items->Item as $Item) {

$array1[(string)$Item->ASIN] = (string)$Item->SmallImage->URL;
}
[/CODE]


works perfectly. the array returns every ASIN and its associated URL as key value pairs.
×

Success!

Help @ivanwakeup spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...