Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-15-2009, 01:07 AM   PM User | #1
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
Exclamation adding xml data to an array only if its not already there?

Hi all,

i am trying to insert data from an xml file into an array only if it is not currently in the array

but its not working as i hoped

here is my code
PHP Code:
foreach($obj->Items->Item as $item)
{
    if(isset(
$item->ItemAttributes->Platform))
    { 
        print 
$item->ItemAttributes->Platform;
        print 
"<br>";

        
$platformArray = array();
        
$x 0;

            if(!
in_array($item->ItemAttributes->Platform,$platformArray))
            {
                
# add platform to $platformArray Array
                
$platformArray[$x] = $item->ItemAttributes->Platform;
                
$x++;
            }
                else
                {
                }    
    }
}

print_r($platformArray); 
print $item->ItemAttributes->Platform; is printing out all the correct info

Code:
PLAYSTATION 3
Xbox 360
PlayStation2
Nintendo Wii
Windows Vista
Sony PSP
Nintendo DS
PLAYSTATION 3
Xbox 360
PLAYSTATION 3
so that part is working ok its just when it comes to adding the data into the array

the output of print_r($platform) is
Code:
Array ( [0] => SimpleXMLElement Object ( [0] => PLAYSTATION 3 ) )
any ideas as to how i can get an array which consists of
Xbox 360
PlayStation2
Nintendo Wii
Windows Vista
Sony PSP
Nintendo DS
PLAYSTATION 3

so that each of the pieces of data appears in the array once.

any ideas please
Luke

Last edited by LJackson; 09-16-2009 at 12:17 AM..
LJackson is offline   Reply With Quote
Old 09-15-2009, 01:46 AM   PM User | #2
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
ok i have made some progress

i relised that i was setting $x to 0 for each time i looped through the loop

so i now have this
PHP Code:
$platformArray = array();
$x 0;

foreach(
$obj->Items->Item as $item)
{
    if(isset(
$item->ItemAttributes->Platform))
    { 
        print 
$item->ItemAttributes->Platform;
        print 
"<br>";

            if(!
in_array($item->ItemAttributes->Platform,$platformArray))
            {
                
# add platform to $platformArray Array
                
$platformArray[$x] = $item->ItemAttributes->Platform;
                
$x++;
            }
    }
}

print_r($platformArray); 
and am printing out this
Code:
PLAYSTATION 3
Xbox 360
PlayStation2
Nintendo Wii
Windows Vista
Sony PSP
Nintendo DS
PLAYSTATION 3
Xbox 360
PLAYSTATION 3

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] => PLAYSTATION 3 ) )
but this is including multiple values which i dont want, i only want each occurence to appear once in the array, how can i achieve this???

thanks
Luke

Last edited by LJackson; 09-16-2009 at 12:19 AM..
LJackson is offline   Reply With Quote
Old 09-15-2009, 02:30 PM   PM User | #3
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
i think it may be because,

the contents of the array are being stored as:
SimpleXMLElement Object ( [0] => Nintendo Wii )
instead of just Nintendo Wii

but im not sure?

here is a sample of 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-Xbox-360/dp/B001HWB68K%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB001HWB68K</DetailPageURL>
   <ItemLinks>
    <ItemLink>
     <Description>Technical Details</Description>
     <URL>http://www.amazon.com/Halo-3-ODST-Xbox-360/dp/tech-data/B001HWB68K%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB001HWB68K</URL>
    </ItemLink>
    <ItemLink>
     <Description>Add To Baby Registry</Description>
     <URL>http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3DB001HWB68K%26SubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB001HWB68K</URL>
    </ItemLink>
    <ItemLink>
     <Description>Add To Wedding Registry</Description>
     <URL>http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3DB001HWB68K%26SubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB001HWB68K</URL>
    </ItemLink>
    <ItemLink>
     <Description>Add To Wishlist</Description>
     <URL>http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3DB001HWB68K%26SubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB001HWB68K</URL>
    </ItemLink>
    <ItemLink>
     <Description>Tell A Friend</Description>
     <URL>http://www.amazon.com/gp/pdp/taf/B001HWB68K%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB001HWB68K</URL>
    </ItemLink>
    <ItemLink>
     <Description>All Customer Reviews</Description>
     <URL>http://www.amazon.com/review/product/B001HWB68K%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB001HWB68K</URL>
    </ItemLink>
    <ItemLink>
     <Description>All Offers</Description>
     <URL>http://www.amazon.com/gp/offer-listing/B001HWB68K%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%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-Evolved-Xbox/dp/B00005NZ1G%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB00005NZ1G</DetailPageURL>
   <ItemLinks>
    <ItemLink>
     <Description>Technical Details</Description>
     <URL>http://www.amazon.com/Halo-Combat-Evolved-Xbox/dp/tech-data/B00005NZ1G%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00005NZ1G</URL>
    </ItemLink>
    <ItemLink>
     <Description>Add To Baby Registry</Description>
     <URL>http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3DB00005NZ1G%26SubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00005NZ1G</URL>
    </ItemLink>
    <ItemLink>
     <Description>Add To Wedding Registry</Description>
     <URL>http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3DB00005NZ1G%26SubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00005NZ1G</URL>
    </ItemLink>
    <ItemLink>
     <Description>Add To Wishlist</Description>
     <URL>http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3DB00005NZ1G%26SubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00005NZ1G</URL>
    </ItemLink>
    <ItemLink>
     <Description>Tell A Friend</Description>
     <URL>http://www.amazon.com/gp/pdp/taf/B00005NZ1G%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00005NZ1G</URL>
    </ItemLink>
    <ItemLink>
     <Description>All Customer Reviews</Description>
     <URL>http://www.amazon.com/review/product/B00005NZ1G%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00005NZ1G</URL>
    </ItemLink>
    <ItemLink>
     <Description>All Offers</Description>
     <URL>http://www.amazon.com/gp/offer-listing/B00005NZ1G%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%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-First-Strike-Reach/dp/0345473043%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0345473043</DetailPageURL>
   <ItemLinks>
    <ItemLink>
     <Description>Technical Details</Description>
     <URL>http://www.amazon.com/Books-Flood-First-Strike-Reach/dp/tech-data/0345473043%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0345473043</URL>
    </ItemLink>
    <ItemLink>
     <Description>Add To Baby Registry</Description>
     <URL>http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3D0345473043%26SubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0345473043</URL>
    </ItemLink>
    <ItemLink>
     <Description>Add To Wedding Registry</Description>
     <URL>http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3D0345473043%26SubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0345473043</URL>
    </ItemLink>
    <ItemLink>
     <Description>Add To Wishlist</Description>
     <URL>http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3D0345473043%26SubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0345473043</URL>
    </ItemLink>
    <ItemLink>
     <Description>Tell A Friend</Description>
     <URL>http://www.amazon.com/gp/pdp/taf/0345473043%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0345473043</URL>
    </ItemLink>
    <ItemLink>
     <Description>All Customer Reviews</Description>
     <URL>http://www.amazon.com/review/product/0345473043%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0345473043</URL>
    </ItemLink>
    <ItemLink>
     <Description>All Offers</Description>
     <URL>http://www.amazon.com/gp/offer-listing/0345473043%3FSubscriptionId%3D144RR0T0YM45X6SVKSR2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%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>
can anyone help please
Luke

Last edited by LJackson; 09-15-2009 at 02:50 PM..
LJackson is offline   Reply With Quote
Old 09-15-2009, 09:42 PM   PM User | #4
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
can this be moved to the xml section please? probably better suited there

thanks
LJackson is offline   Reply With Quote
Old 09-16-2009, 01:55 PM   PM User | #5
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
ok i have solved this now
LJackson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:16 AM.


Advertisement
Log in to turn off these ads.