[Wylug-help] PHP foreach help

William F. McCaw william at mcsquawk.org.uk
Thu Nov 19 16:24:11 UTC 2009


Hi Gary,

On Thursday 19 November 2009 15:02:13 Gary Stainburn wrote:
[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.
[snip]

   In that case the problem would seem to be somewhere in the code that's not 
shown I'm afraid...  Have you tried adding error logging to check 
the "$dets['tab']" value at the start of the second loop ?

   This is a complete stab in the dark here but, the first loop isn't located 
in a different function to the second, with the $List array being passed in 
as a parameter is it ?  If so, is the array being passed in by reference 
rather than by value (the latter case would mean the changes by loop 1 to the 
copy of the array within the function would be lost when the function ended).

   I dropped your code fragment into the following small test script to double 
check the array referencing (I only removed the "....." lines).

--8<----------8<----------8<----------8<----------8<----------8<----------8<--------8<--

<?php

function paint_line($dets)
   { }

$User = array( 'defaults' => array( 'ff_tab' => 'live' ) );
$List = array();

array_push($List, array( 'current' => 't' ));
array_push($List, array( 'current' => 'f' ));
//array_push($List, array( 'current' => '-' ));   // Uncomment line for error


foreach ($List as &$dets) {
  if ($dets['current'] == 't') { $dets['tab']='live';}
  if ($dets['current'] == 'f') { $dets['tab']='dead';}
} # foreach

foreach ($List as &$dets){
  if ($User['defaults']['ff_tab'] == $dets['tab'] && !isset($dets['seen'])) {
    paint_line($dets);
    $dets['seen']=1;
  } 
}   
    

print_r($List);
  
?>

--8<----------8<----------8<----------8<----------8<----------8<----------8<--------8<--

   William.

-- 
William F. McCaw  <william at mcsquawk.org.uk>

Public key available from http://www.mcsquawk.org.uk/ and keyservers
GPG : 0x34C648D9 : 2744 C1E8 38AE FC6F 38CC  1D1E F9E9 D8D2 34C6 48D9



More information about the Wylug-help mailing list