Page 1 of 1

using array of objects in php

Posted: Wed Mar 21, 2007 3:12 am
by rekha_harnoor
Hi All,

Following is my code snippet.
I am parsing a xml and displaying. But I want to store 'ProductName', 'PriceFrom', 'PriceTo' to an array of objects and then display it.
How do I do that?

Code: Select all

for($i=0; $i<$res['totalResultsReturned']; $i++) {
	foreach($pob->Result[$i] as $key=>$value) {
	 echo "<a href=\"{$value->Url}\" target=\"blank\" title=\"Learn More at Yahoo.com\">{$value->ProductName}<a>";
                 echo "  Min. Price $" . "{$value->PriceFrom}";
                 echo "  Max. Price $" . "{$value->PriceTo}";
	 echo "<hr />\n";
	      
				
	}

}

Posted: Wed Mar 21, 2007 3:20 am
by dude81

Code: Select all

$items_list=array();
for($i=0; $i<$res['totalResultsReturned']; $i++) { 
        foreach($pob->Result[$i] as $key=>$value) {
                   echo "<a href=\"{$value->Url}\" target=\"blank\" title=\"Learn More at Yahoo.com\">{$value->ProductName}<a>"; 
                 echo "  Min. Price $" . "{$value->PriceFrom}"; 
                 echo "  Max. Price $" . "{$value->PriceTo}"; 
         echo "<hr />\n"; 
   $items_list[$i]['product_name'] = $value->ProductName;
   $items_list[$i]['price_from'] = $value->PriceFrom;
   $items_list[$i]['price_to'] = $value->PriceTo;               
                                
        } 

} 
print_r($items_list);
Hopefully is this what you are expecting??

Re: using array of objects in php

Posted: Wed Mar 21, 2007 6:43 pm
by harrisonad
you wrote:...But I want to store 'ProductName', 'PriceFrom', 'PriceTo' to an array of objects and then display it.
adding object to an array is not a big deal. it's just like adding values to an array in a usual way.

Code: Select all

$items = array();
$items[] = new ItemClass($prod_name,$price_from,$price_to);
and displaying it is just like..

Code: Select all

echo $items[0]->getProducName();
i hope that helped