An array of array pointers

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
liderbug
Forum Newbie
Posts: 19
Joined: Tue Jul 23, 2002 2:18 pm

An array of array pointers

Post by liderbug »

Cant get the code to work. I populate several arrays with info: id, name, dob, etc. Each array is different person.
FF, FM, F, C, M, MF, MM .. ancestry stuff

Code: Select all

 
$FF = array ('123', 'b', 'c');
$FM = array ('124', 'b', 'c');
$F = array ('125', 'b', 'c');
 
$list = array ($FF, $FM, $F, ...);
 
foreach ($list as $arr) {
   get_person ($arr[0]);   # $arr[0] should be 123 etc
}
 
at least that's the idea. Just can't get the duck's in proper order.

Thanks
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: An array of array pointers

Post by Weirdan »

what does the following show:

Code: Select all

$FF = array ('123', 'b', 'c');
$FM = array ('124', 'b', 'c');
$F = array ('125', 'b', 'c');
 
$list = array ($FF, $FM, $F, ...);
 
foreach ($list as $arr) {
  var_dump($arr[0]);
}
?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: An array of array pointers

Post by requinix »

If get_person is supposed to modify $arr and therefore modify $FF/FM/F then it won't work:

Code: Select all

$list = array ($FF, $FM, $F, ...);
$list contains a copy of $FF, $FM, and $F.

Code: Select all

foreach ($list as $arr) {
$arr is a copy of each element in the $list array.

Code: Select all

get_person ($arr[0]);
Unless your function is defined correctly, get_person will modify a copy of $arr[0].

You need to learn about references.
Post Reply