Jump to content

array within an array within an array


grissom
Go to solution Solved by 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.

Edited by grissom
Link to comment
Share on other sites

  • Solution

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);
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.