Jump to content

advancing the array index pointer


mbeals

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/116065-advancing-the-array-index-pointer/
Share on other sites

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'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.