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
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'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.