Newbie: pulling data from array of objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dry_aquaman
Forum Newbie
Posts: 3
Joined: Mon Mar 16, 2009 5:42 pm

Newbie: pulling data from array of objects

Post by dry_aquaman »

I'm a bit of a Newbie but I need to work something out that I'm struggling with. I've tried to RTFM but ain't getting any where.
I'm trying to access a data feed from CJ.
The call goes ok but the results are giving me grief because it appears to be an array of objects.
I need to be able to pull the results from each of the objects.

If I do a print_r($results); the output looks something like this:

Code: Select all

stdClass Object ( [out] => stdClass Object ( [count] => 2 [offset] => 0 [products] => stdClass Object ( [Product] => Array ( [0] => stdClass Object ( [adId] => 10356001 [advertiserId] => 1055136 [advertiserName] => 4inkjets! [buyUrl] => http://www.kqzyfj.com/click-2351998-103 ... =XPI-4080C [catalogId] => cjo:628 [currency] => USD [description] => Save money with the Inktec Color Ink Refill Kit for Xerox 8R7880 Ink-jet Cartridges. [imageUrl] => http://www.ldproducts.com/varien/media/ ... -4080C.jpg [inStock] => [isbn] => [manufacturerName] => Xerox [manufacturerSku] => 8R7880 [name] => Inktec Color Ink Refill Kit for Xerox 8R7880 Ink-jet Cartridges [price] => 18.99 [retailPrice] => 0 [salePrice] => 14.99 [sku] => XPI-4080C [upc] => )
What do I need to do to be able to access each of the object variables?

Dry_Aquaman
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Newbie: pulling data from array of objects

Post by panic! »

post your code as well as the var dump.

you will prbably need to do something like this

Code: Select all

 
 
 
foreach($results as $item) 
{
 
echo $item->salePrice;
 
echo $item->description;  
}
 
 
 
dry_aquaman
Forum Newbie
Posts: 3
Joined: Mon Mar 16, 2009 5:42 pm

Re: Newbie: pulling data from array of objects

Post by dry_aquaman »

Thanks for the reply.

Here's the code:

Code: Select all

<?php
        //  PID   Used Printer.us = 2351998
        $websiteId = "webid";
 
        // set initial $q
        if ($q=='') $q='ink'; 
        
        // set maximum number of results
        $maxResults='2';
        
        //required to access CJ services    
    $developerKey = "key";
        
        
        /////////////////////////////////////////////////////////////////////////////////
        ///   Code Starts here                                                       ///
        ////////////////////////////////////////////////////////////////////////////////
    $ini = ini_set("soap.wsdl_cache_enabled","0");
 
 
    try {
 
        $client = new SoapClient("https://product.api.cj.com/wsdl/version2/productSearchServiceV2.wsdl", array('trace'=> true));
 
        //Enter the request parameters for productSearch below.
        //For detailed usage of the parameter values, please refer to CJ Web Services online documentation
        
        $results = $client->search(array("developerKey" => $developerKey,
                                            "websiteId" => $websiteId,
                                            "advertiserIds" => '1055136',
                                            "keywords" => $q,
                                            "serviceableArea" => 'US',
                                            "upc" => '',
                                            "linkType" => '',
                                            "manufacturerName" => '',
                                            "manufacturerSku" => '',
                                            "advertiserSku" => '',
                                            "lowPrice" => '',
                                            "highPrice" => '',
                                            "lowSalePrice" => '',
                                            "highSalePrice" => '',
                                            "currency" => 'USD',
                                            "isbn" => '',
                                            "sortBy" => 'price',
                                            "sortOrder" => 'asc',
                                            "startAt" => 0,
                                            "maxResults" => $maxResults));
 
        // The entire response structure will be printed in the next line
        print_r($results);
 
    } catch (Exception $e){
        echo "There was an error with your request or the service is unavailable.\n";
        print_r ($e);
    }
////////////
// Results vars
///////////     
// adId
// advertiserId
// advertiserName
// buyUrl
// catalogId
// currency
// description
// imageUrl
// inStock
// isbn
// manufacturerName
// manufacturerSku
// name
// price
// retailPrice
// salePrice
// sku
// upc  
        
            
        echo '<br>Results Below<br>'; 
        echo 'Next text <br>'; 
         foreach($results as $item)
{
  
 echo $item->salePrice;
  echo '<br>';
echo $item->description;  
 }
?>
The foreach() statement currently just post nulls.

Dry_Aquaman
dry_aquaman
Forum Newbie
Posts: 3
Joined: Mon Mar 16, 2009 5:42 pm

Re: Newbie: pulling data from array of objects

Post by dry_aquaman »

Ping.
Post Reply