Jump to content

array within an array within an array


grissom

Recommended Posts

Hi guys.  This one should be really simple but I've got a mental block on it and would really appreciate some help.

 

I'm using some classes from an existing open source class EZPDF to format a table, and their syntax for doing so is like this (here's an example .. here we take an array called $data with fields 'Date', 'Name' and 'Postcode' and assign widths to the columns)

$pdf->ezTable($data,'','',array('showHeadings'=>1,'shaded'=>1,'showLines'=>1,
                                        'cols'=>array('Date'=>array('width'=>120),
                                        'Name'=>array('width'=>100),
                                        'Postcode'=>array('width'=>80))
                                        ));

Well, that looked a little bit too much work for my lazy brain, so I decided to extend the class and write a new function which basically read in all the data keys and matched them to an array of widths and then did the same thing. But with more convenient shorthand for me !

 

So here's how I approached it :

function basictableformat($data, $widthsarray) {
                $basicarray = array();
                $i = 0;
                foreach ($data[0] as $key=>$value) {
                        $widthbit = array('width'=>$widthsarray[$i]);
                        $basicarray[$key] = $widthbit;
                        $i++;
                        }
                $this->ezTable($data,'','',array('showHeadings'=>1,'shaded'=>1,'showLines'=>1,
                                        'cols'=>array(
                                        $basicarray
                                        )
                                        ));
                }

However, as you can guess .. this didn't work !  It should be EASY but I've just got a Sunday morning mental block on it. Any help folks is massively appreciated.

Link to comment
https://forums.phpfreaks.com/topic/297362-array-within-an-array-within-an-array/
Share on other sites

The problem is that although no errors are being thrown up, the table is not formatting with the column widths I am specifying

 

As a bit of extra info, here's an example of how I would set up and call the function

 

 

$data = array();
$data[] = array('Date'=>'12-12-2015', 'Name'=>'Alice', 'Postcode'=>'HG1');
$data[] = array('Date'=>'25-11-2015', 'Name'=>'Bob', 'Postcode'=>'SE2');
$widths = array(100, 120, 60);
basictableformat($data, $widths);

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.