Page 1 of 1

An array of array pointers

Posted: Tue Feb 10, 2009 1:50 pm
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

Re: An array of array pointers

Posted: Tue Feb 10, 2009 2:10 pm
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]);
}
?

Re: An array of array pointers

Posted: Tue Feb 10, 2009 3:36 pm
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.