Page 1 of 1

array and arrayobject woes

Posted: Tue Jun 28, 2005 1:09 pm
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.

Posted: Tue Jun 28, 2005 1:28 pm
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.

Posted: Wed Jun 29, 2005 7:05 pm
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())
                );
   }
?>