mbeals Posted July 22, 2008 Share Posted July 22, 2008 I have a function that builds a table row by row, using logic to determine whether a row should be added or not. To simplify the task, I built a function that accepts an array of 'cells' and returns the html for the table. It's structure is: buildRow(array $cells, string $alignment, string $cellColor) What I need to do is generate an array of hashes, where each hash contains the three elements for a row. Something like: Array ( [0] => Array ( [data] => Array ( [0] => cell 1 [1] => cell 2 [2] => cell 3 [3] => cell 4 ) [opts] => center [color] => #CCFFFF ) [1] => Array ( [data] => Array ( [0] => cell 1 [1] => cell 2 [2] => cell 3 [3] => cell 4 ) [opts] => right [color] => #CCFFFF ) ) Traditionally I would build the top level array like: $rows = array(); $rows[] = array('cell 1', 'cell 2', 'cell 3', 'cell 4'); $rows[] = ..... and just allow the array pointer to auto advance. However....when I add the associative hash in, it doesn't work: <?php $rows = array(); $rows[]['data'] = array('cell 1', 'cell 2', 'cell 3', 'cell 4'); $rows[]['opts'] = 'center'; $rows[]['color'] = '#CCFFFF'; ?> Doesn't produce the desired affect The closest I came was with this: <?php $rows[]['data'] = array('cell 1', 'cell 2', 'cell 3', 'cell 4'); $rows[key($rows)]['opts'] = 'center'; $rows[key($rows)]['color'] = ''; $rows[]['data'] = array('cell 1', 'cell 2', 'cell 3', 'cell 4'); $rows[key($rows)]['opts'] = 'center'; $rows[key($rows)]['color'] = '#CCFFFF'; ?> but it fails as well because for some reason key perpetually returns 0 as the array position. Am I missing something obvious? I did look at using a temp variable, but would like to avoid it if possible. Quote Link to comment Share on other sites More sharing options...
s0c0 Posted July 22, 2008 Share Posted July 22, 2008 Build your array like this: $tableArr[]=array('data'=>array(0=>'cell 1',1=>'cell 2'),'opts'=>'center','color'=>'#FFF'); Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 22, 2008 Author Share Posted July 22, 2008 thought about it, but the 'data' array can be quite long (contains html) and clarity-wise that gets ugly in a hurry. It's also not that flexible if I need to add more keys later on. I haven't found documentation yet, but I think using the [] shortcut is just an alias for array_push(), so when I add something with that syntax, it is getting pushed onto the stack, but the pointer is left unchanged. I ended up just calling end() to advance the pointer and it seems to be working. ie: $rows[]['data'] = array('cell 1', 'cell 2', 'cell 3', 'cell 4'); end($rows); $rows[key($rows)]['opts'] = 'center'; $rows[key($rows)]['color'] = ''; not sure if there is an easier way to handle it though....so I'll leave the thread 'unsolved' Quote Link to comment Share on other sites More sharing options...
s0c0 Posted July 23, 2008 Share Posted July 23, 2008 Then create a single dimensional array called data, add the values in and then throw it in with the rest of the suff. That cleans up the code. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.