using array of objects in php

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
rekha_harnoor
Forum Commoner
Posts: 32
Joined: Mon Feb 19, 2007 3:17 am

using array of objects in php

Post 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";
	      
				
	}

}
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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??
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Re: using array of objects in php

Post 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
Post Reply