Arrays in PHP seems to drop elements

Go To StackoverFlow.com

1

I am building an app with complex data which I use arrays for. Some of the data comes direct from fetchs from a postgresql data base and some comes from calculation and processing.

I initally build the array that holds all this and everything goes along fine.

Later during processing I have the following code. This is one section of my code.

    foreach ($this -> detailTables as $key => $detTable) {
        foreach ($detTable['columns'] as $key2 => $column) {
            if ($column['isID'] == 't') {
                $detTable['mainid'] = $column['columnName'];
            }
            if ($column['lookupTable'] != '') {
                $select = array('table' => $column['lookupTable'], 'id' => $column['lookupID'], 'disp' => $column['lookupDisplay']);
                $detTable['lookups'][] = $select;
            }
        }
        logw($key . ' mainid 1', $detTable['mainid']);
        logw($key.' lookups 1', $detTable['lookups']);
    }

    foreach ($this -> detailTables as $key => $detTable) {
        logw($key . ' mainid 2', $detTable['mainid']);
        logw($key.' lookups 2', $detTable['lookups']);
    }

Basically I am scanning detailTables for and their columns to determine and set values for mindid and the lookups array

In the second foreach segment I am just logging the values to see if the persist.

My Log file reads as follows.

columns mainid 1 = columnID
array columns lookups 1
 array 0
  table = tables
  id = tableID
  disp = tableName

tabs mainid 1 = tabID
array tabs lookups 1
 array 0
  table = tables
  id = tableID
  disp = tableName

and the output from the second part reads

columns mainid 2
columns lookups 2
tabs mainid 2
tabs lookups 2

I cannot figure out why the two log sections are not identical.

2012-04-03 19:26
by farley


1

From my understanding of you structure you need to reset the iteration after the first for each through

    }
    logw($key . ' mainid 1', $detTable['mainid']);
    logw($key.' lookups 1', $detTable['lookups']);
}
//reset variables here
foreach ($this -> detailTables as $key => $detTable) {
    logw($key . ' mainid 2', $detTable['mainid']);
    logw($key.' lookups 2', $detTable['lookups']);
}
2012-04-03 19:36
by RyanS


0

$detTable should be a reference. So you should change first line into

 foreach ($this -> detailTables as $key => &$detTable) {
2012-04-03 19:34
by Jarosław Gomułka
Ads