试试这个:
<?php
foreach($inputs as $label => $data){
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
$this->Cell(100, 7, $data[0], 0, 0, 'L', 1, 0);
$this->SetFillColor($data[1]);
}
或者更好的方法是创建一个\stdClass对象,如下:
<?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);
}
}
编辑:
你也可以这样做:
<?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);
甚至可以更加面向对象地完成这个任务。
<?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();
}