Hi There -
I have three mysql tables. Am performing a join on two of them on an order_id then creating an array of arrays from each row. (Each row getting its own array.) The result is a series of arrays whose prin_r looks like this...
newitems = Array
(
[0] => Array
(
[buyer_name] => mary
[message] => yo!
[item_title] => door
[nid] => 3539
)
[1] => Array
(
[buyer_name] => jane
[message] => yo!
[item_title] => screen
[nid] => 3540
)
)
Nothing terribly exciting and that part's working great.
On the third table, if a condition is met (if there exists an 'nid' in the array) then I do a second query to get another number associated with the nid, the refid. This is in a third table.
If there exists a refid, then I need to add it to the listed array of arrays so it would look like this:
newitems = Array
(
[0] => Array
(
[buyer_name] => mary
[message] => yo!
[item_title] => door
[nid] => 3539
[refid] => 9397 <-------------- refid needs to go here!
)
[1] => Array
(
[buyer_name] => jane
[message] => yo!
[item_title] => screen
[nid] => 3540
[refid] => 9395 <-------------- refid!
)
)
so I did a foreach loop over the item arrays...
<?php
foreach ($newitems as $newitem){
if ($newitem[nid]) {
//DB QUERY GOES HERE to get $refid
$newitem[refid] = $refid;
//print_r $newitem GOES HERE <----------first print_r
}
}
//print_r $newitems GOES HERE <----------second print_r
return $newitems
?>Here's what's weird: if I do a print_r on the $newitem arrays inside the foreach (the first print_r), so that each one gets printed out separately, then refid is added to each individual array. That's exactly what they're supposed to do.
But they don't stick! When I print_r the $newitems array (the second print_r) - no refid! It's the same as before I did the foreach loop to add the refid. It's just like the first example above!
Gah!
What am I doing wrong? I've been fiddling with this for more than a day and I'm flummoxed.
I'll be happy to scrap the entire approach I'm using in favor of one that works. Is there a way I can include the addition of the refid during the while loop that's parsing through the first join query? Wouldn't need a foreach then.
I'd love to be able to generalize, how do you address a sequence of arrays when you don't know the middle term? (ie: $newitems[????][refid])
Comments
I suspect this has to do
I suspect this has to do with the second "Note" here and the following text (foreach works with a copy, except if a reference is used):
http://php.net/foreach
Thanks for your reply...
Am really confused by references. I've read about them on drupal.org and a little bit elsewhere, but seem to be missing a key bit of the puzzle, like when to use them and why, and how being a reference to a variable is somehow more direct than using the variable itself. Perhaps its the nomenclature, but a reference seems to be more indirect by just the word. . . unless the variable that I'm so familiar with is even less direct than I thought it was.
And how do you find out that you're using a copy in the foreach?!
OMG! It's all a fiction! Arrhhhhgh!!
My world is crumbling.
The combination of lack of sleep and the realization that my safe little world doesn't have the boundaries I thought it did means I should probably take a break or I'll have some kind of crisis all over the page.
Are there php counselors for this stuff? A hotline with sympathetic programmers on the other end to help me get through this?
A red pill?
Very helpful rabbit hole. Thank you!
On this page of the PHP manual, it shows how to use references when looping through an array.
http://us.php.net/manual/en/language.references.php
jasonpvp at gmail dot com
10-Jul-2008 07:16
To change values in a multi-dimensional array while looping through:
$var=array('a'=>array(1,2,3),'b'=>array(4,5,6));
foreach ($var as &$sub) {
foreach ($sub as &$element) {
$element=$element+1;
}
}
var_dump($var);
------------------------------
produces:
------------------------------
array(2) {
["a"]=>
array(3) {
[0]=>
int(2)
[1]=>
int(3)
[2]=>
int(4)
}
["b"]=>
array(3) {
[0]=>
int(5)
[1]=>
int(6)
[2]=>
int(7)
}
}
Hope someone else besides me finds this helpful.
Heh, PHP is tricky. I have
Heh, PHP is tricky. I have heard of "deep copies" and "shallow copies" of arrays, objects which behave differently in PHP4 and PHP5 (drupal even has a drupal_clone() function for that)... go figure.