[Wylug-help] PHP foreach help
Roger
roger at roger-beaumont.co.uk
Thu Nov 19 18:22:19 UTC 2009
Gary Stainburn wrote:
> On Thursday 19 November 2009 14:38:31 William F. McCaw wrote:
>> Hi Gary,
>>
>> On Thursday 19 November 2009 13:33:54 Gary Stainburn wrote:
>>> According to the doc's, referencing $dets in the first loop should make
>>> any changes made there appear in the second loop, i.e.
>>>
>>> foreach ($List as &$dets) {
>>> if ($dets['current'] == 't') { $dets['tab']='live';}
>>> if ($dets['current'] == 'f') { $dets['tab']='dead';}
>>> ....
>>> } # foreach
<snip>>> .....
>>> However, I'm getting
>>>
>>> PHP Notice: Undefined index: tab in /home/httpd/webroot/funding1.html
>>> on line 88,
>>>
>>> Line 88 is the if statement inside the second look.
>>>
>>> Can anyone see what I've done wrong.
<snip>>
> I have simplified the code here, but I have tried setting
>
> $dets['tab']='';
>
> before the if statements, and also put an error_log immediately after the two
> if statements. every row does have a valid value in $dets['tab'] before the
> end of the itteration in the first look.
You'll get sick of looking at it, but put
echo "\$List = <pre>", print_r($List,1), "</pre>"; just inside the first
loop. Since you are using &$dets, after each iteration you should see
the new values in $List.
Also put the same line immediately after closing that loop - and you'll
find if the changes are happening to the original, or just to a local
copy within the loop.
If so, then you may have to use a 'for' loop to iterate explicitly
through all the elements of $List (and use $List[$i]['tab'] instead of
$dets['tab'] of course). If the indices of $List are numeric, that's
easy. If not, create another array, say $indices that DOES use numeric
indices to reference whatever the indices of $List actually are.
I've occasionally found that foreach should work, but doesn't, but never
managed to discover why or how, so I haven't reported the bug (sometimes
it has worked at first, then stopped working when other code was added,
elsewhere, to the script!)
I've had the problem, but can't describe it more explicitly than
"Sometimes, foreach doesn't work!"
Good luck,
Roger
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
More information about the Wylug-help
mailing list