/    Sign up×
Community /Pin to ProfileBookmark

xml, loops and array help please.

Hi All,

ok i have been trying to get this to work for ages now with no luck. im trying to pullout pieces of data from an xml url and add specific data into an array. which is working ok ?

i also want to make it so that each array value is unique so that any duplicate entries are not added to the array, this is not working ?

here is my code

[code=php]$platformArray = array();
$x = 0;

foreach($obj->Items->Item as $item)
{
$platform = $item->ItemAttributes->Platform;
if(isset($platform))
{
print “<p>”;
print $platform;
print “<br>”;
print_r($platform);
print “</p>”;

if(!in_array($platform,$platformArray))
{
# add platform to $platformArray Array
$platformArray[] = $platform;
//$x++;
}
}
}

print_r($platformArray);
[/code]

and here is the output

[CODE]PLAYSTATION 3
SimpleXMLElement Object ( [0] => PLAYSTATION 3 )

Xbox 360
SimpleXMLElement Object ( [0] => Xbox 360 )

PlayStation2
SimpleXMLElement Object ( [0] => PlayStation2 )

Nintendo Wii
SimpleXMLElement Object ( [0] => Nintendo Wii )

Windows Vista
SimpleXMLElement Object ( [0] => Windows Vista )

Sony PSP
SimpleXMLElement Object ( [0] => Sony PSP )

Nintendo DS
SimpleXMLElement Object ( [0] => Nintendo DS )

PLAYSTATION 3
SimpleXMLElement Object ( [0] => PLAYSTATION 3 )

Xbox 360
SimpleXMLElement Object ( [0] => Xbox 360 )

Xbox 360
SimpleXMLElement Object ( [0] => Xbox 360 )
Array ( [0] => SimpleXMLElement Object ( [0] => PLAYSTATION 3 ) [1] => SimpleXMLElement Object ( [0] => Xbox 360 ) [2] => SimpleXMLElement Object ( [0] => PlayStation2 ) [3] => SimpleXMLElement Object ( [0] => Nintendo Wii ) [4] => SimpleXMLElement Object ( [0] => Windows Vista ) [5] => SimpleXMLElement Object ( [0] => Sony PSP ) [6] => SimpleXMLElement Object ( [0] => Nintendo DS ) [7] => SimpleXMLElement Object ( [0] => PLAYSTATION 3 ) [8] => SimpleXMLElement Object ( [0] => Xbox 360 ) [9] => SimpleXMLElement Object ( [0] => Xbox 360 ) ) [/CODE]

as you can see when printing out the array im getting:
[9] =>SimpleXMLElement Object ( [0] => Xbox 360 )

insted of
[9] => Xbox 360

can anyone please help me solve this?

here is a snippet for the xml file

[CODE] <ItemSearchResponse>
<OperationRequest>
<RequestId>10444268-ebdd-4e6f-9b8d-8e4917b5aac4</RequestId>
<Arguments>
<Argument Name=”Operation” Value=”ItemSearch” />
<Argument Name=”Service” Value=”AWSECommerceService” />
<Argument Name=”Signature” Value=”” />
<Argument Name=”Version” Value=”2009-03-01″ />
<Argument Name=”Keywords” Value=”Halo” />
<Argument Name=”AWSAccess” />
<Argument Name=”AWSAccessKeyId” Value=”” />
<Argument Name=”Timestamp” Value=”2009-09-15T13:46:47Z” />
<Argument Name=”SearchIndex” Value=”VideoGames” />
</Arguments>
<RequestProcessingTime>0.1190980000000000</RequestProcessingTime>
</OperationRequest>
<Items>
<Request>
<IsValid>True</IsValid>
<ItemSearchRequest>
<Condition>New</Condition>
<DeliveryMethod>Ship</DeliveryMethod>
<Keywords>Halo</Keywords>
<MerchantId>Amazon</MerchantId>
<ResponseGroup>Small</ResponseGroup>
<ReviewSort>-SubmissionDate</ReviewSort>
<SearchIndex>VideoGames</SearchIndex>
</ItemSearchRequest>
</Request>
<TotalResults>151</TotalResults>
<TotalPages>16</TotalPages>
<Item>
<ASIN>B001HWB68K</ASIN>
<DetailPageURL>http://www.amazon.com/Halo-3-ODST-Xb…N%3DB001HWB68K</DetailPageURL>
<ItemLinks>
<ItemLink>
<Description>Technical Details</Description>
<URL>http://www.amazon.com/Halo-3-ODST-Xb…N%3DB001HWB68K</URL>
</ItemLink>
<ItemLink>
<Description>Add To Baby Registry</Description>
<URL>http://www.amazon.com/gp/registry/ba…N%3DB001HWB68K</URL>
</ItemLink>
<ItemLink>
<Description>Add To Wedding Registry</Description>
<URL>http://www.amazon.com/gp/registry/we…N%3DB001HWB68K</URL>
</ItemLink>
<ItemLink>
<Description>Add To Wishlist</Description>
<URL>http://www.amazon.com/gp/registry/wi…N%3DB001HWB68K</URL>
</ItemLink>
<ItemLink>
<Description>Tell A Friend</Description>
<URL>http://www.amazon.com/gp/pdp/taf/B00…N%3DB001HWB68K</URL>
</ItemLink>
<ItemLink>
<Description>All Customer Reviews</Description>
<URL>http://www.amazon.com/review/product…N%3DB001HWB68K</URL>
</ItemLink>
<ItemLink>
<Description>All Offers</Description>
<URL>http://www.amazon.com/gp/offer-listi…N%3DB001HWB68K</URL>
</ItemLink>
</ItemLinks>
<ItemAttributes>
<Manufacturer>Microsoft</Manufacturer>
<ProductGroup>Video Games</ProductGroup>
<Title>Halo 3: ODST</Title>
</ItemAttributes>
</Item>
<Item>
<ASIN>B00005NZ1G</ASIN>
<DetailPageURL>http://www.amazon.com/Halo-Combat-Ev…N%3DB00005NZ1G</DetailPageURL>
<ItemLinks>
<ItemLink>
<Description>Technical Details</Description>
<URL>http://www.amazon.com/Halo-Combat-Ev…N%3DB00005NZ1G</URL>
</ItemLink>
<ItemLink>
<Description>Add To Baby Registry</Description>
<URL>http://www.amazon.com/gp/registry/ba…N%3DB00005NZ1G</URL>
</ItemLink>
<ItemLink>
<Description>Add To Wedding Registry</Description>
<URL>http://www.amazon.com/gp/registry/we…N%3DB00005NZ1G</URL>
</ItemLink>
<ItemLink>
<Description>Add To Wishlist</Description>
<URL>http://www.amazon.com/gp/registry/wi…N%3DB00005NZ1G</URL>
</ItemLink>
<ItemLink>
<Description>Tell A Friend</Description>
<URL>http://www.amazon.com/gp/pdp/taf/B00…N%3DB00005NZ1G</URL>
</ItemLink>
<ItemLink>
<Description>All Customer Reviews</Description>
<URL>http://www.amazon.com/review/product…N%3DB00005NZ1G</URL>
</ItemLink>
<ItemLink>
<Description>All Offers</Description>
<URL>http://www.amazon.com/gp/offer-listi…N%3DB00005NZ1G</URL>
</ItemLink>
</ItemLinks>
<ItemAttributes>
<Manufacturer>Microsoft</Manufacturer>
<ProductGroup>Video Games</ProductGroup>
<Title>Halo: Combat Evolved</Title>
</ItemAttributes>
</Item>
<Item>
<ASIN>0345473043</ASIN>
<DetailPageURL>http://www.amazon.com/Books-Flood-Fi…N%3D0345473043</DetailPageURL>
<ItemLinks>
<ItemLink>
<Description>Technical Details</Description>
<URL>http://www.amazon.com/Books-Flood-Fi…N%3D0345473043</URL>
</ItemLink>
<ItemLink>
<Description>Add To Baby Registry</Description>
<URL>http://www.amazon.com/gp/registry/ba…N%3D0345473043</URL>
</ItemLink>
<ItemLink>
<Description>Add To Wedding Registry</Description>
<URL>http://www.amazon.com/gp/registry/we…N%3D0345473043</URL>
</ItemLink>
<ItemLink>
<Description>Add To Wishlist</Description>
<URL>http://www.amazon.com/gp/registry/wi…N%3D0345473043</URL>
</ItemLink>
<ItemLink>
<Description>Tell A Friend</Description>
<URL>http://www.amazon.com/gp/pdp/taf/034…N%3D0345473043</URL>
</ItemLink>
<ItemLink>
<Description>All Customer Reviews</Description>
<URL>http://www.amazon.com/review/product…N%3D0345473043</URL>
</ItemLink>
<ItemLink>
<Description>All Offers</Description>
<URL>http://www.amazon.com/gp/offer-listi…N%3D0345473043</URL>
</ItemLink>
</ItemLinks>
<ItemAttributes>
<Author>Eric Nylund</Author>
<Manufacturer>Del Rey</Manufacturer>
<ProductGroup>Book</ProductGroup>
<Title>Halo, Books 1-3 (The Flood; First Strike; The Fall of Reach)</Title>
</ItemAttributes>
</Item>
</Items>
</ItemSearchResponse> [/CODE]

many thanks
Luke

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@msd_lukeauthorSep 16.2009 — its ok i have managed to solve this problem.

kept the original array with multiple values and then added a new array to get just the unique values like so
[code=php]$array2 = array_unique($platformArray);[/code]

which works a treat ?
Copy linkTweet thisAlerts:
@MindzaiSep 16.2009 — Do you needs 2 arrays? Why not just:

[code=php]$platformArray = array_unique($platformArray);[/code]
Copy linkTweet thisAlerts:
@msd_lukeauthorSep 16.2009 — good point, thanks
×

Success!

Help @msd_luke 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 5.27,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...