array and arrayobject woes

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
hthb
Forum Newbie
Posts: 5
Joined: Sun May 15, 2005 1:49 pm

array and arrayobject woes

Post by hthb »

I have a C# background so I'm not very familiar with the array type in php.

I'm trying to APPEND to an array. A similar code with ArrayObject works:

Code: Select all

$aoResult = new ArrayObject();

	foreach($compteam as $ct)	
			{
				
				$comp = $ct->getCompetition();
				
				$aoResult->append(array(
				'teamid' => $ct->getTeamid(),
				'teamname' => $team->getTeamname(),
				'compid' => $comp->getCompetitionid(),
				'compname' => $comp->getName(),
				'type' => $comp->getType(),
				'endtime' => $comp->getEndtime('Y-m-d H:i:s'),
				'endmoney' => $comp->getEndmoney(),
				'nrofteams' => $this->getTeamCount($ct->getTeamid()), 

}
How can I replace the ArrayObject with an Array?

I have tried:

Code: Select all

$testarray = array();

	foreach($compteam as $ct)	
			{
				$testarray(array(
				'teamid' => $ct->getTeamid(),
				'teamname' => $team->getTeamname(),
				'compid' => $comp->getCompetitionid(),
				'compname' => $comp->getName(),
				'type' => $comp->getType(),
				'endtime' => $comp->getEndtime('Y-m-d H:i:s'),
				'endmoney' => $comp->getEndmoney(),
				'nrofteams' => $this->getTeamCount($ct->getTeamid()), 
				
				));

}
I get the message: Fatal error: Function name must be a string for the $testarray(array(... line

Any help would be appreciated.

Thanks.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

to add to an array just use

Code: Select all

$bob = array();
$bob['key'] = $value;
//or
$bob[] = $value;
so you'd have to define your array outside your foreach loop then just use something like above to add to its keys/values.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

Code: Select all

<?
    $testarray = array();
 
    foreach($compteam as $ct) {
                $testarray[] = array(
                'teamid' => $ct->getTeamid(),
                'teamname' => $team->getTeamname(),
                'compid' => $comp->getCompetitionid(),
                'compname' => $comp->getName(),
                'type' => $comp->getType(),
                'endtime' => $comp->getEndtime('Y-m-d H:i:s'),
                'endmoney' => $comp->getEndmoney(),
                'nrofteams' => $this->getTeamCount($ct->getTeamid())
                );
   }
?>
Post Reply