java to php : arraylists and hashmaps

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
shibby1011ph
Forum Newbie
Posts: 2
Joined: Mon Feb 21, 2005 6:47 am

java to php : arraylists and hashmaps

Post 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.
}
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

you would have to define 'getter functions' for each of the fields in class much like Java itself :)
shibby1011ph
Forum Newbie
Posts: 2
Joined: Mon Feb 21, 2005 6:47 am

Post by shibby1011ph »

i already did..i dont understand why it generates an error
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

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