php: foreach on array does not move to next item

Wim Delvaux wim.delvaux at adaptiveplanet.com
Wed Oct 24 23:01:44 UTC 2007


Package: php
Severity: grave
Justification: renders package unusable


Look at this code :

    ...
print_r($Planning);

    foreach( $Planning as $SeqNr => &$Plan ) {

      $Tbl->newDataSet();
      $Tbl->setColumns( array(
        'EmployeeID' => $Plan['Employee'],
        'CustomerID' => $CID[0][0],
        'PlannedDateStart' => $Plan['PlannedDateStart'],
        'PlannedDateEnd' => $Plan['PlannedDateStart'],
        'PlannedStart' => $Plan['PlannedTimeStart'],
        'PlannedEnd' => $Plan['PlannedTimeEnd'],
        'AssignmentID' => $Plan['AssignmentID'],
        'ServiceID' => $Plan['Service'],
        'ServicePrice' => $Services[$Plan['Service']]
      ) );

print_r($Plan);
    }

This is the output :

The Planning array ...

Array
(
    [0] => Array
        (
            [AssignmentID] => 20071018-99107
            [Employee] => 20070427-56628
            [Service] => 20060828-130
            [PlannedDateStart] => 2007-10-25
            [PlannedTimeStart] => 2
            [PlannedTimeEnd] => 7
        )

    [1] => Array
        (
            [AssignmentID] => 20071018-99107
            [Employee] => 20060516-617
            [Service] => 20060828-130
            [PlannedDateStart] => 2007-10-25
            [PlannedTimeStart] => 3
            [PlannedTimeEnd] => 7
        )

)

loop 1

Array
(
    [AssignmentID] => 20071018-99107
    [Employee] => 20070427-56628
    [Service] => 20060828-130
    [PlannedDateStart] => 2007-10-25
    [PlannedTimeStart] => 2
    [PlannedTimeEnd] => 7
)

loop 2 
Array
(
    [AssignmentID] => 20071018-99107
    [Employee] => 20070427-56628
    [Service] => 20060828-130
    [PlannedDateStart] => 2007-10-25
    [PlannedTimeStart] => 2
    [PlannedTimeEnd] => 7
)

see that the foreach takes the same item ???


if I change the code to

  foreach( $Planning as $SeqNr => $Plan ) {
    ...
  }

code acts as expected, taking the NEXT item in the Planning array

-- System Information:
Debian Release: 4.0
  APT prefers feisty-updates
  APT policy: (500, 'feisty-updates'), (500, 'feisty-security'), (500, 'feisty')
Architecture: i386 (i686)
Shell:  /bin/sh linked to /bin/dash
Kernel: Linux 2.6.20-16-generic
Locale: LANG=en_US.ISO-8859-15, LC_CTYPE=en_US.ISO-8859-15 (charmap=ISO-8859-15)




More information about the ubuntu-users mailing list