Page 1 of 1

java to php : arraylists and hashmaps

Posted: Mon Feb 21, 2005 6:56 am
by shibby1011ph
how to go through an array without using a pointer?
like looping through arrays in php just like in java iterators?

$i=0
while ($i < $personList.size()-1){
$person = $personList[$i]; // i don't wanna use that. it's just like an average java array. i want it to be something like the java iterator class.
echo $person->getFirstName();
}

i read that foreach is usually used in arrays but what if it's used in user-defined objects?

$p = new Person();
foreach($personList as $person){
$p = $person;
echo $p->getCurrentSalary()."<br>"; //this generates an error. i want to retrieve the objects in the arraylist just as they are.
}

Posted: Mon Feb 21, 2005 7:21 am
by n00b Saibot
you would have to define 'getter functions' for each of the fields in class much like Java itself :)

Posted: Mon Feb 21, 2005 5:52 pm
by shibby1011ph
i already did..i dont understand why it generates an error

Posted: Mon Feb 21, 2005 6:00 pm
by feyd

Code: Select all

$p = null;
foreach($personList as $person)
&#123;
  $p =& $person;
  $g = $p->getCurrentSalary();
  echo $g . '<br />';
&#125;
your way, I believe, requires php5.