Reputation: 2893
at the moment I pass one of my functions an array like so
$inputs = array("FOOD" => "Pancake");
And then in my function I do something like the following
foreach ($inputs as $label => $data)
{
echo ($label . $data);
}
The above is only an example I have just written so I hope it is correct. Anyways, the function which has that foreach loop also has the following within it
$this->SetFillColor(190,205,44);
Now for each loop, I also want to set the fill colour of the output. So I was thinking about doing something like the following instead
$inputs = array("FOOD" => array("Pancake", '190,205,44'));
If I var_dump the array I am passing my function, I see
array:1 [▼
"FOOD" => array:2 [▼
0 => "PANCAKE"
1 => "190,205,44"
]
]
So this all seems correct. Now in my function I do
foreach($inputs as $label => $data){
foreach($data as $content => $colour) {
$this->SetFillColor($colour);
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
$this->Cell(100, 7, $content, 0, 0, 'L', 1, 0);
}
}
Now I have a couple of problems. Firstly, it seems to output two rows. The first cell prints out FOOD like it should do but the second cell prints out 0 (should be PANCAKE). The second row once again prints out FOOD, but the second cell prints out 1.
So how can I get it printing out just the first row? The other problem is $colour, because this is a string when it needs to be an int I think.
Any advice appreciated.
Thanks
Upvotes: 3
Views: 79
Reputation: 641
Try this:
<?php
foreach($inputs as $label => $data){
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
// content is on index 0
$this->Cell(100, 7, $data[0], 0, 0, 'L', 1, 0);
// color on index 1
$this->SetFillColor($data[1]);
}
Or maybe even better create an object of \stdClass like this:
<?php
$foo[0]['food'] = new \stdClass;
$foo[0]['food']->content= 'Pancake';
$foo[0]['food']->color = '190,205,44';
$foo[1]['food'] = new \stdClass;
$foo[1]['food']->content= 'Apple';
$foo[1]['food']->color = '255,0,0';
for($i=0,$cnt=count($foo);$i<$cnt;$i++) {
foreach($foo[$i] as $label => $data) {
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
$this->Cell(100, 7, $data->content, 0, 0, 'L', 1, 0);
$this->SetFillColor($data->color);
}
}
Edit: You can do this too:
<?php
$foo[0]['food']->color = new \stdClass;
$foo[0]['food']->content= 'Pancake';
$foo[0]['food']->color->r = 190;
$foo[0]['food']->color->g = 205;
$foo[0]['food']->color->b = 44;
... [snip]
$c = $data->color;
$this->SetFillColor($c->r,$c->g,$c->b);
Or even do it more OOP style
<?php
class color {
private $r;
private $g;
private $b;
public function get_r() {
return (float) $this->r;
}
public function get_g() {
return (float) $this->g;
}
public function get_b() {
return (float) $this->b;
}
public function set_r($r) {
$this->r = $r;
return $this;
}
public function set_g($g) {
$this->g = $g;
return $this;
}
public function set_b($b) {
$this->b = $b;
return $this;
}
static public function factory() {
return new color;
}
public function __construct() {
}
}
class food {
private $content;
private $color;
public function get_content() {
return $this->content;
}
public function get_color() {
return $this->color;
}
public function set_content($content) {
$this->content = $content;
return $this;
}
public function set_color(color $color) {
$this->color = $color;
return $this;
}
static public function factory() {
return new food;
}
public function __construct() {
}
}
$color = color::factory()->set_r(255)->set_g(0)->set_b(0);
$dataarray[] = food::factory()->set_content('Apple')->set_color($color);
foreach($dataarray as $food) {
echo $food->get_content();
echo $food->get_color()->get_r();
echo $food->get_color()->get_g();
echo $food->get_color()->get_b();
}
Upvotes: 4
Reputation: 2311
I think you wanted to write
$inputs = array("FOOD" => array("Pancake" => '190,205,44'));
so that 'Pancake'
is a key that has a color string as its value.
Otherwise you should adjust your code to read like this:
foreach($inputs as $label => $data){
$content = $data[0];
$color = $data[1];
$this->SetFillColor($colour);
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
$this->Cell(100, 7, $content, 0, 0, 'L', 1, 0);
}
Either way should do the trick.
Currently $inputs = array("FOOD" => array("Pancake", '190,205,44'));
just sets the 'FOOD'
entry of $inputs
to contain an array that has keys of 0,1
and so your $content
variable will iterate 0,1
while $color
iterates the values 'PANCAKE','190,205,44'
.
Upvotes: 3